#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int maxn = 1e3 + 5;
struct Point {
  double x, y;
  Point(double x = 0, double y = 0) : x(x), y(y) {}
} p[maxn];
Point operator-(Point A, Point B) { return Point(A.x - B.x, A.y - B.y); }
const double eps = 1e-5;
int sgn(double x) {
  if (fabs(x) < eps) return 0;
  if (x < 0) return -1;
  return 1;
}
double Dot(Point A, Point B) { return A.x * B.x + A.y * B.y; }
double Length(Point A) { return sqrt(Dot(A, A)); }
double Cross(Point A, Point B) { return A.x * B.y - A.y * B.x; }
struct node {
  double len;
  int cs;
  node(double len = 0, int cs = 0) : len(len), cs(cs) {}
  bool operator<(const node& nd) const { return len < nd.len; }
};
double cal(Point A, Point B, Point C, Point D) {
  double ret = Cross(C - A, D - C) / Cross(B - A, D - C);
  return ret;
}
vector<node> v;
double solve(int n, Point A, Point B) {
  double len = Length(A - B);
  v.clear();
  for (int i = 0; i < n; ++i) {
    int a = sgn(Cross(p[i] - A, B - A));
    int b = sgn(Cross(p[(i + 1) % n] - A, B - A));
    if (a == b) continue;
    v.push_back(node(len * cal(A, B, p[i], p[(i + 1) % n]), a - b));
  }
  int sz = (int)v.size();
  sort(v.begin(), v.end());
  int wn = 0;
  double ret = 0;
  for (int i = 0; i < sz - 1; ++i) {
    wn += v[i].cs;
    if (wn) ret += v[i + 1].len - v[i].len;
  }
  return ret;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) scanf("%lf%lf", &p[i].x, &p[i].y);
  while (m--) {
    Point A, B;
    scanf("%lf%lf%lf%lf", &A.x, &A.y, &B.x, &B.y);
    double ans = solve(n, A, B);
    printf("%.20f\n", ans);
  }
  return 0;
}
