#include <bits/stdc++.h>
using namespace std;
bool pcmp(complex<long double> a, complex<long double> b) {
  return (fabs(a.real() - b.real()) > 1e-7L && a.real() < b.real()) ||
         (fabs(a.real() - b.real()) < 1e-7L &&
          fabs(a.imag() - b.imag()) > 1e-7L && a.imag() < b.imag());
}
inline bool pointOnSegment(complex<long double> a, complex<long double> b,
                           complex<long double> p) {
  return (((((b) - (a)).real()) * (((p) - (a)).real())) +
          ((((b) - (a)).imag()) * (((p) - (a)).imag()))) > 1e-4L &&
         (((((a) - (b)).real()) * (((p) - (b)).real())) +
          ((((a) - (b)).imag()) * (((p) - (b)).imag()))) > 1e-7L;
}
complex<long double> lineIntersection(complex<long double> a1,
                                      complex<long double> b1,
                                      complex<long double> a2,
                                      complex<long double> b2) {
  long double A1 = a1.imag() - b1.imag(), B1 = b1.real() - a1.real(),
              A2 = a2.imag() - b2.imag(), B2 = b2.real() - a2.real();
  long double C1 = A1 * a1.real() + B1 * a1.imag(),
              C2 = A2 * a2.real() + B2 * a2.imag(), det = A1 * B2 - B1 * A2;
  if (fabs(det) < 1e-7L) return complex<long double>(1e+9, 1e+9);
  return complex<long double>(C1 * B2 - B1 * C2, A1 * C2 - C1 * A2) / det;
}
void order(vector<complex<long double> > &v) {
  long double area = 0;
  int sz = v.size();
  for (int i = 1; i + 1 < sz; i++)
    area += ((conj(((v[i]) - (v[0]))) * (((v[i + 1]) - (v[0])))).imag());
  if (area < -1e-7L) reverse(v.begin(), v.end());
}
int main() {
  int n, m;
  long double x1, y1, x2, y2;
  cout << setprecision(11) << fixed;
  cin >> n >> m;
  vector<complex<long double> > vec(n);
  map<complex<long double>, int,
      bool (*)(complex<long double>, complex<long double>)>
      vertex(pcmp);
  for (int i = 0; i < n; i++) {
    cin >> x1 >> y1;
    vec[i] = complex<long double>(x1, y1);
  }
  order(vec);
  for (int i = 0; i < n; i++) vertex[vec[i]] = i;
  while (m--) {
    long double com = 0;
    cin >> x1 >> y1 >> x2 >> y2;
    complex<long double> a = complex<long double>(x1, y1),
                         b = complex<long double>(x2, y2);
    vector<complex<long double> > inter;
    for (int i = 0; i < n; i++)
      if (fabs(((conj(((b) - (a))) * (((vec[i]) - (a)))).imag())) < 1e-7L) {
        int j = (i - 1 + n) % n;
        int k = (i + 1) % n;
        if (((conj(((b) - (a))) * (((vec[j]) - (a)))).imag()) *
                ((conj(((b) - (a))) * (((vec[k]) - (a)))).imag()) <
            1e-7L)
          inter.push_back(vec[i]);
      }
    for (int i = 0; i < n; i++) {
      int j = (i + 1) % n;
      complex<long double> d = lineIntersection(vec[i], vec[j], a, b);
      if (pcmp(d, complex<long double>(1e+9, 1e+9)) &&
          pointOnSegment(vec[i], vec[j], d))
        inter.push_back(d);
    }
    sort(inter.begin(), inter.end(), pcmp);
    int sz = inter.size();
    bool f = 1;
    for (int i = 0; i < sz - 1; i++) {
      bool flag = 1;
      if (f)
        flag = 0, com += (hypotl((((inter[i + 1]) - (inter[i]))).imag(),
                                 (((inter[i + 1]) - (inter[i]))).real()));
      f = !f;
      if (vertex.count(inter[i]) && vertex.count(inter[i + 1])) {
        int a = vertex[inter[i]], b = vertex[inter[i + 1]], c = b + 1;
        int d = abs(a - b);
        if (d != 1 && d != n - 1) continue;
        if (a > b && b) {
          c = a;
          a = b - 1;
        }
        if (flag)
          com += (hypotl((((inter[i + 1]) - (inter[i]))).imag(),
                         (((inter[i + 1]) - (inter[i]))).real()));
        f = (((conj(((vec[b]) - (vec[a]))) * (((vec[c]) - (vec[a])))).imag()) <
             1e-7L);
      }
    }
    cout << com << endl;
  }
  return 0;
}
