#include <bits/stdc++.h>
using namespace std;
long long read() {
  long long a = 0, b = getchar(), c = 1;
  while (!isdigit(b)) c = b == '-' ? -1 : 1, b = getchar();
  while (isdigit(b)) a = a * 10 + b - '0', b = getchar();
  return a * c;
}
const int N = 3005;
struct no {
  long double x, y;
  void in() { cin >> x >> y; }
  no operator-(const no &b) { return (no){x - b.x, y - b.y}; }
  no operator*(const no &b) { return (no){x * b.x, y * b.y}; }
  long double operator^(const no &b) { return x * b.y - y * b.x; }
} p[N], a, b;
int n, m, x, y, q;
vector<pair<long double, int> > c;
long double ans;
int jj(long double x) { return x < -(1e-8) ? -1 : x > (1e-8); }
long double gg(no u, no v, no w) { return (w ^ u) / (v ^ w); }
long double gao(no x, no v) {
  c.clear();
  for (int i = 1; i <= n; i++) {
    int f1 = jj(v ^ (p[i] - x)), f2 = jj(v ^ (p[i + 1] - x));
    if (f1 == f2) continue;
    long double o = gg(x - p[i], v, p[i + 1] - p[i]);
    if (f1 > f2)
      c.push_back(make_pair(o, f1 and f2 ? 2 : 1));
    else
      c.push_back(make_pair(o, f1 and f2 ? -2 : -1));
  }
  sort(c.begin(), c.end());
  ans = 0;
  for (int i = 0, j = 0; i + 1 < c.size(); i++)
    if (j += c[i].second, j) ans += c[i + 1].first - c[i].first;
  return ans * hypotl(v.x, v.y);
}
int main() {
  n = read(), q = read();
  for (int i = 1; i <= n; i++) p[i].in();
  p[n + 1] = p[1];
  while (q--) {
    a.in(), b.in();
    printf("%.15lf\n", (double)gao(a, b - a));
  }
  return 0;
}
