#include <bits/stdc++.h>
using namespace std;
const long double EPS = 1e-8;
const long double INF = 1e12;
const long double PI = acos(-1);
struct L : array<complex<long double>, 2> {
  L(const complex<long double>& a, const complex<long double>& b) {
    at(0) = a;
    at(1) = b;
  }
  L() {}
};
namespace std {
bool operator<(const complex<long double>& a, const complex<long double>& b) {
  return !(abs((a.real()) - (b.real())) < EPS) ? a.real() < b.real()
                                               : a.imag() + EPS < b.imag();
}
bool operator==(const complex<long double>& a, const complex<long double>& b) {
  return abs(a - b) < EPS;
}
}  // namespace std
long double dot(complex<long double> a, complex<long double> b) {
  return (conj(a) * b).real();
}
long double cross(complex<long double> a, complex<long double> b) {
  return (conj(a) * b).imag();
}
bool intersectLP(const L& l, const complex<long double>& p) {
  return abs(cross(l[1] - p, l[0] - p)) < EPS;
}
bool strictItsLS(const L& l, const L& s) {
  return cross(l[1] - l[0], s[0] - l[0]) * cross(l[1] - l[0], s[1] - l[0]) <
         -EPS;
}
bool isParallel(const complex<long double>& a, const complex<long double>& b) {
  return abs(cross(a, b)) < EPS;
}
bool isParallel(const L& a, const L& b) {
  return isParallel(a[1] - a[0], b[1] - b[0]);
}
complex<long double> crosspointLL(const L& l, const L& m) {
  long double A = cross(l[1] - l[0], m[1] - m[0]);
  long double B = cross(l[1] - l[0], l[1] - m[0]);
  return m[0] + B / A * (m[1] - m[0]);
}
long double getarea(const vector<complex<long double> >& poly) {
  long double ret = 0;
  for (int i = 0; i < (int)poly.size(); i++) {
    ret += cross(poly[i], poly[(i + 1) % poly.size()]);
  }
  return ret * 0.5;
}
bool in_corner(const complex<long double>& p,
               const vector<complex<long double> >& v) {
  bool s1 = cross(v[1] - v[0], p - v[0]) > -EPS;
  bool s2 = cross(v[2] - v[1], p - v[1]) > -EPS;
  return (cross(v[1] - v[0], v[2] - v[0]) > -EPS) ? s1 and s2 : s1 or s2;
}
long double solve(vector<complex<long double> >& v, L cut) {
  int n = v.size();
  complex<long double> cdir = cut[1] - cut[0];
  vector<complex<long double> > cps;
  for (int i = 0; i < n; i++) {
    L edge(v[i], v[(i + 1) % n]);
    if (strictItsLS(cut, edge) and !isParallel(cut, edge)) {
      cps.push_back(crosspointLL(cut, edge));
    }
    if (intersectLP(cut, v[i])) {
      vector<complex<long double> > p{v[(i - 1 + n) % n], v[i], v[(i + 1) % n]};
      if (in_corner(p[1] + cdir, p) != in_corner(p[1] - cdir, p)) {
        cps.push_back(v[i]);
      }
    }
  }
  sort(cps.begin(), cps.end());
  long double res = 0;
  for (int i = 0; i < (int)cps.size() - 1; i += 2) {
    res += abs(cps[i + 1] - cps[i]);
  }
  return res;
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<complex<long double> > v(n);
  for (int i = 0; i < n; i++) {
    long double x, y;
    cin >> x >> y;
    v[i] = complex<long double>(x, y);
  }
  if (getarea(v) < 0) {
    reverse(v.begin(), v.end());
  }
  cout << fixed << setprecision(10);
  for (int i = 0; i < m; i++) {
    long double xs, ys, xt, yt;
    cin >> xs >> ys >> xt >> yt;
    cout << solve(v,
                  L(complex<long double>(xs, ys), complex<long double>(xt, yt)))
         << endl;
  }
  return 0;
}
