#include <bits/stdc++.h>
double cross2(double x1, double y1, double x2, double y2) {
  return x1 * y2 - x2 * y1;
}
double cross(double x0, double y0, double x1, double y1, double x2, double y2) {
  return (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0);
}
double dot(double x1, double y1, double x2, double y2) {
  return x1 * x2 + y1 * y2;
}
double intersect(double x1, double y1, double x2, double y2, double x3,
                 double y3, double x4, double y4, int get_x) {
  double t = cross2(x3 - x1, y3 - y1, x4 - x3, y4 - y3) /
             cross2(x2 - x1, y2 - y1, x4 - x3, y4 - y3);
  return get_x ? x1 + (x2 - x1) * t : y1 + (y2 - y1) * t;
}
int main() {
  static double xx[1000 + 1], yy[1000 + 1];
  int n, q, i, j;
  double a, tmp;
  scanf("%d%d", &n, &q);
  for (i = 0; i < n; i++) scanf("%lf%lf", &xx[i], &yy[i]);
  xx[n] = xx[0], yy[n] = yy[0];
  a = 0;
  for (i = 0; i < n; i++) a += cross2(xx[i], yy[i], xx[i + 1], yy[i + 1]);
  if (a > 0)
    for (i = 1, j = n - 1; i < j; i++, j--)
      tmp = xx[i], xx[i] = xx[j], xx[j] = tmp, tmp = yy[i], yy[i] = yy[j],
      yy[j] = tmp;
  while (q--) {
    double x1, y1, x2, y2, ans;
    scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
    ans = 0;
    for (i = 0; i < n; i++) {
      double c1 = cross(x1, y1, x2, y2, xx[i], yy[i]),
             c2 = cross(x1, y1, x2, y2, xx[i + 1], yy[i + 1]);
      if (c1 == 0 && c2 == 0) {
        if (dot(xx[i + 1] - xx[i], yy[i + 1] - yy[i], x2 - x1, y2 - y1) > 0)
          ans += x1 == x2 ? yy[i + 1] - yy[i] : xx[i + 1] - xx[i];
      } else if (c1 <= 0 && c2 > 0)
        ans -= intersect(x1, y1, x2, y2, xx[i], yy[i], xx[i + 1], yy[i + 1],
                         x1 != x2);
      else if (c1 > 0 && c2 <= 0)
        ans += intersect(x1, y1, x2, y2, xx[i], yy[i], xx[i + 1], yy[i + 1],
                         x1 != x2);
    }
    ans = ans * (sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))) /
          (x1 == x2 ? y2 - y1 : x2 - x1);
    printf("%f\n", ans);
  }
  return 0;
}
