#include <bits/stdc++.h>
int n, m;
typedef struct {
  double x, y, ex, ey;
  char sign;
} Vertex;
Vertex vertices[1001];
typedef struct StackFrame {
  double t;
  char sign;
} StackFrame;
StackFrame stack[1001];
int cmp(const void *a, const void *b) {
  return ((StackFrame *)a)->t > ((StackFrame *)b)->t ? 1 : -1;
}
void solve() {
  double x0, y0, x1, y1, res = 0;
  scanf("%lf%lf%lf%lf", &x0, &y0, &x1, &y1);
  x1 -= x0;
  y1 -= y0;
  for (Vertex *v = vertices + n; v != vertices; v--) {
    double product = x1 * (v->y - y0) - y1 * (v->x - x0);
    v->sign = (product < -1e-5) ? -1 : (product > 1e-5) ? 1 : 0;
  }
  vertices[0].sign = vertices[n].sign;
  int sp = 0;
  for (Vertex *v = vertices + n; v != vertices; v--)
    if (stack[sp].sign = v->sign - (v - 1)->sign)
      stack[sp++].t = ((v->x - x0) * v->ey - (v->y - y0) * v->ex) /
                      (x1 * v->ey - y1 * v->ex);
  qsort(stack, sp, sizeof(StackFrame), cmp);
  for (int sign = 0; sp;)
    if (sign += stack[--sp].sign) res += stack[sp].t - stack[sp - 1].t;
  printf("%.10f\n", res * sqrt(x1 * x1 + y1 * y1));
}
int main() {
  scanf("%d%d", &n, &m);
  for (Vertex *v = vertices + n; v != vertices; v--)
    scanf("%lf%lf", &(v->x), &(v->y));
  vertices[0] = vertices[n];
  for (Vertex *v = vertices + n; v != vertices; v--) {
    v->ex = v->x - (v - 1)->x;
    v->ey = v->y - (v - 1)->y;
  };
  while (m--) solve();
}
