I will try to show the solution is ceil(M / N+1) where M is the larger of B and G, N is the lesser.
Best case is M = N, where we need merely arrange the genders alternately, and regularity = ceil(M / M+1) = 1. Worst case is a single-gender class, i.e N=0 in which regularity = ceil(M/1) = M, i.e. the whole class size. In all other cases, clearly 1 < regularity < M+N.
Now work quasi-inductively. If N = 1, then we place the singleton in the center of the line, with half of the others to each side. If the larger group is even, there are M/2 to each side (the ceil of a whole number is itself.) If the larger group is odd, there will be one more on one side of the single, and it is this group that gives regularity = ceil(M/2).
If N = 2, do a similar process, divide the M into three groups, placing the two in between them (picture ---------- where the * are the small group, - the larger). If the larger group is divisible by 3, there are M/3 in each division. If not, distribute the 1 or 2 remainder over distinct divisions, and the largest will contain ceil(M/3) students.
As N increases toward M, repeat the concept, dividing the larger group into M/N+1 divisions, place the N between them, and distribute the remainder.