#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long infLL = 0x3f3f3f3f3f3f3f3fLL;
const double eps = 1e-10;
int sgn(double x) { return x < -eps ? -1 : x > eps; }
double dot(complex<double> u, complex<double> v) {
  return u.real() * v.real() + u.imag() * v.imag();
}
double cross(complex<double> u, complex<double> v) {
  return u.real() * v.imag() - u.imag() * v.real();
}
const int maxn = 1000 + 5;
int n, m;
complex<double> g[maxn];
int sz;
pair<complex<double>, int> vec[maxn];
struct cmp {
  complex<double> s, t;
  cmp(complex<double> src, complex<double> trg) : s(src), t(trg) {}
  bool operator()(pair<complex<double>, int> u,
                  pair<complex<double>, int> v) const {
    complex<double> a = u.first, b = v.first;
    return sgn(a.real() - b.real())
               ? sgn(a.real() - b.real()) == sgn(s.real() - t.real())
               : sgn(a.imag() - b.imag()) == sgn(s.imag() - t.imag());
  }
};
bool check(complex<double> v, complex<double> v1, complex<double> v2) {
  double c1 = cross(v1, v), c2 = cross(v, v2);
  return sgn(cross(v1, v2)) <= 0 ? sgn(c1) <= 0 && sgn(c2) <= 0
                                 : !(sgn(c1) > 0 && sgn(c2) > 0);
}
double inter(complex<double> s, complex<double> t) {
  sz = 0;
  for (int i = 0; i < (n); ++i) {
    complex<double> a = g[i], b = g[(i + 1) % n];
    double c1 = cross(a - s, t - s), c2 = cross(b - s, t - s);
    if (!sgn(c1))
      vec[sz++] = make_pair(a, n + i);
    else if ((sgn(c1) ^ sgn(c2)) == -2)
      vec[sz++] = make_pair((c1 * b - c2 * a) / (c1 - c2), i);
  }
  sort(vec, vec + sz, cmp(s, t));
  double ret = 0;
  for (int j = 0; j < (sz - 1); ++j) {
    complex<double> v = vec[j + 1].first - vec[j].first;
    if (vec[j].second < n) {
      int i = vec[j].second;
      if (sgn(cross(g[(i + 1) % n] - g[i], v)) >= 0) ret += abs(v);
    } else {
      int i = vec[j].second - n;
      if (check(v, g[(i - 1 + n) % n] - g[i], g[(i + 1) % n] - g[i]))
        ret += abs(v);
    }
  }
  return ret;
}
int main() {
  scanf("%d%d", &n, &m);
  double x, y;
  for (int i = 0; i < (n); ++i) {
    scanf("%lf%lf", &x, &y);
    g[i] = complex<double>(x, y);
  }
  double area = 0;
  for (int i = 1; i < n - 1; ++i) area += cross(g[i] - g[0], g[i + 1] - g[0]);
  if (sgn(area) < 0) reverse(g, g + n);
  while (m--) {
    scanf("%lf%lf", &x, &y);
    complex<double> s = complex<double>(x, y);
    scanf("%lf%lf", &x, &y);
    complex<double> t = complex<double>(x, y);
    printf("%.10f\n", inter(s, t));
  }
  return 0;
}
