#include <bits/stdc++.h>
using namespace std;
const int mn = 1e3 + 5;
int n, m;
namespace Geometry {
const double eps = 1e-10;
double add(double a, double b) {
  if (fabs(a + b) < eps * (fabs(a) + fabs(b))) return 0;
  return a + b;
}
struct point {
  double x, y;
  point() {}
  point(double x, double y) : x(x), y(y) {}
  point operator+(point p) { return point(add(x, p.x), add(y, p.y)); }
  point operator-(point p) { return point(add(x, -p.x), add(y, -p.y)); }
  point operator*(double d) { return point(x * d, y * d); }
  double dot(point p) { return add(x * p.x, y * p.y); }
  double det(point p) { return add(x * p.y, -y * p.x); }
} p[mn];
bool on_seg(point p1, point p2, point q) {
  return (p1 - q).det(p2 - q) == 0 && (p1 - q).dot(p2 - q) <= 0;
}
point intersection(point p1, point p2, point q1, point q2) {
  return p1 + (p2 - p1) * ((q2 - q1).det(q1 - p1) / (q2 - q1).det(p2 - p1));
}
double PositionofIntersection(point p1, point p2, point q1, point q2) {
  return ((q2 - q1).det(q1 - p1) / (q2 - q1).det(p2 - p1));
}
bool Parallel(point p1, point p2, point q1, point q2) {
  return (p1 - p2).det(q1 - q2) == 0;
}
}  // namespace Geometry
using namespace Geometry;
int sgn(double p) {
  if (p < 0) return -1;
  return p > 0;
}
double solve(point a, point b) {
  vector<pair<double, int> > v;
  for (int i = 1; i <= n; i++) {
    int k1 = sgn(b.det(p[i] - a));
    int k2 = sgn(b.det(p[i + 1] - a));
    if (k1 == k2) continue;
    double u = PositionofIntersection(a, a + b, p[i], p[i + 1]);
    int w = sgn((p[i + 1] - p[i]).det(b));
    if (abs(k1) && k2 == 0) {
      v.push_back(make_pair(u, w));
    } else if (abs(k2) && k1 == 0) {
      v.push_back(make_pair(u, w));
    } else {
      v.push_back(make_pair(u, 2 * w));
    }
  }
  sort(v.begin(), v.end());
  double ans = 0;
  int sign = 0;
  for (int i = 0; i < (int)v.size() - 1; i++) {
    sign += v[i].second;
    if (sign) ans += v[i + 1].first - v[i].first;
  }
  return ans * hypot(b.x, b.y);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%lf%lf", &p[i].x, &p[i].y);
  p[n + 1] = p[1];
  while (m--) {
    point a, b;
    scanf("%lf%lf%lf%lf", &a.x, &a.y, &b.x, &b.y);
    printf("%.20f\n", solve(a, b - a));
  }
  return 0;
}
