#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-10;
int n, m;
struct Point {
  double x, y;
  Point() {}
  Point(double x, double y) : x(x), y(y) {}
};
Point P[1005];
Point operator-(const Point &a, const Point &b) {
  return Point(a.x - b.x, a.y - b.y);
}
int get(double x) {
  if (fabs(x) < eps) return 0;
  return x < 0 ? -1 : 1;
}
double det(Point a, Point b) { return a.x * b.y - b.x * a.y; }
double getIntersection(Point a1, Point a2, Point b1, Point b2) {
  return det(b2 - b1, a1 - b1) / det(b2 - b1, a2 - a1);
}
double solve(Point A, Point B) {
  priority_queue<pair<double, int>, vector<pair<double, int> >,
                 greater<pair<double, int> > >
      record;
  for (int i = 0; i < n; i++) {
    int t1 = get(det(B - A, P[i] - A));
    int t2 = get(det(B - A, P[i + 1] - A));
    if (t1 == t2) continue;
    double x = getIntersection(A, B, P[i], P[i + 1]);
    record.push(pair<double, int>(x, t1 - t2));
  }
  double ans = 0;
  int sign = 0;
  while (record.size() > 1) {
    sign += record.top().second;
    double tmp = record.top().first;
    record.pop();
    if (sign) ans += record.top().first - tmp;
  }
  return ans * sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y));
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%lf%lf", &P[i].x, &P[i].y);
  P[n] = P[0];
  double x1, y1, x2, y2;
  for (int i = 0; i < m; i++) {
    scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
    double ans = solve(Point(x1, y1), Point(x2, y2));
    printf("%.20f\n", ans);
  }
  return 0;
}
