#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
struct Point {
  double x, y;
  Point(double _ = 0, double __ = 0) { x = _, y = __; }
  bool operator<(const Point &_) const {
    if (fabs(x - _.x) > 1e-10) return x < _.x;
    return y < _.y;
  }
} po[N], rec[N * 2], stk[N * 2];
struct Line {
  Point a, b;
} L;
struct Mem {
  Point a;
  int type;
  bool operator<(const Mem &_) const {
    if (fabs(a.x - _.a.x) > 1e-10) return a.x < _.a.x;
    if (fabs(a.y - _.a.y) > 1e-10) return a.y < _.a.y;
    return type < _.type;
  }
} pp[N * 2];
int n, m;
inline double xmul(Point a, Point b, Point c) {
  return (a.x - b.x) * (a.y - c.y) - (a.y - b.y) * (a.x - c.x);
}
inline bool across(Point &a, Point &b, Point &c, Point &d) {
  double p1 = xmul(a, b, c), p2 = xmul(a, b, d);
  if (fabs(p1) < 1e-10 || fabs(p2) < 1e-10) return 1;
  if (p1 * p2 < 1e-10) return 1;
  return 0;
}
inline bool oneline(Point &a, Point &b, Point &c, Point &d) {
  double p1 = xmul(a, b, c), p2 = xmul(a, b, d);
  if (fabs(p1) < 1e-10 && fabs(p2) < 1e-10) return 1;
  return 0;
}
inline Point intersection(Line &u, Line &v) {
  Point ret = u.a;
  double t =
      ((u.a.x - v.a.x) * (v.a.y - v.b.y) - (u.a.y - v.a.y) * (v.a.x - v.b.x)) /
      ((u.a.x - u.b.x) * (v.a.y - v.b.y) - (u.a.y - u.b.y) * (v.a.x - v.b.x));
  ret.x += (u.b.x - u.a.x) * t;
  ret.y += (u.b.y - u.a.y) * t;
  return ret;
}
inline double dis(Point &a, Point &b) {
  return sqrt(((a.x - b.x) * (a.x - b.x)) + ((a.y - b.y) * (a.y - b.y)));
}
inline bool onseg(Point &a, Point &b, Point &c) {
  if (fabs(xmul(a, b, c)) < 1e-10) return 1;
  return 0;
}
inline double getans() {
  Point a, b;
  int tot = 0, cnt = 0, top = 0;
  double ans = 0;
  for (int i = 1; i <= n; i++) {
    if (oneline(L.a, L.b, po[i], po[i + 1])) {
      cnt++;
      pp[cnt].a = po[i];
      pp[cnt].type = 1;
      cnt++;
      pp[cnt].a = po[i + 1];
      pp[cnt].type = -1;
      if (pp[cnt].a < pp[cnt - 1].a) swap(pp[cnt].a, pp[cnt - 1].a);
      continue;
    }
    int k1 = onseg(L.a, L.b, po[i]) || xmul(L.a, L.b, po[i]) < 0;
    int k2 = onseg(L.a, L.b, po[i + 1]) || xmul(L.a, L.b, po[i + 1]) < 0;
    if (k1 + k2 != 1) continue;
    if (onseg(L.a, L.b, po[i])) {
      rec[++tot] = po[i];
      continue;
    }
    if (onseg(L.a, L.b, po[i + 1])) {
      rec[++tot] = po[i + 1];
      continue;
    }
    if (across(L.a, L.b, po[i], po[i + 1])) {
      Line tmp;
      tmp.a = po[i], tmp.b = po[i + 1];
      rec[++tot] = intersection(L, tmp);
    }
  }
  sort(rec + 1, rec + tot + 1);
  for (int i = 1; i < tot; i += 2) {
    cnt++;
    pp[cnt].a = rec[i];
    pp[cnt].type = 1;
    cnt++;
    pp[cnt].a = rec[i + 1];
    pp[cnt].type = -1;
  }
  sort(pp + 1, pp + cnt + 1);
  for (int i = 1; i <= cnt; i++) {
    if (pp[i].type == 1)
      stk[++top] = pp[i].a;
    else {
      if (top == 1) ans += dis(stk[top], pp[i].a);
      top--;
    }
  }
  return ans;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%lf%lf", &po[i].x, &po[i].y);
  po[n + 1] = po[1];
  for (int i = 1; i <= m; i++) {
    scanf("%lf%lf%lf%lf", &L.a.x, &L.a.y, &L.b.x, &L.b.y);
    printf("%.8lf\n", getans());
  }
  return 0;
}
