#include <bits/stdc++.h>
using namespace std;
int dcmp(long double a) { return (a > 1e-10) - (a < -1e-10); }
long double dot(complex<long double> a, complex<long double> b) {
  return a.real() * b.real() + a.imag() * b.imag();
}
long double cross(complex<long double> a, complex<long double> b) {
  return a.real() * b.imag() - a.imag() * b.real();
}
long double get(const complex<long double> &p, const complex<long double> &v,
                const complex<long double> &q, const complex<long double> &w) {
  complex<long double> u = p - q;
  return cross(w, u) / cross(v, w);
}
vector<pair<long double, long double> > vec;
void getSegmentInter(const complex<long double> &p,
                     const complex<long double> &v,
                     const complex<long double> &q,
                     const complex<long double> &w) {
  if (dcmp(cross(v, w)) == 0) {
    if (dcmp(cross(v, q - p)) == 0) {
      long double l = dot(v, q - p) / dot(v, v);
      long double r = dot(v, q + w - p) / dot(v, v);
      if (l > r) swap(l, r);
      vec.push_back(make_pair(l, -1.0));
      vec.push_back(make_pair(r, 1.0));
    }
  } else {
    int num = 0;
    long double tmp = get(q, w, p, v);
    if (dcmp(tmp) >= 0 && dcmp(tmp - 1) <= 0) {
      long double rate = 1;
      if (dcmp(tmp) == 0 || dcmp(tmp - 1.0) == 0) rate = 0.5;
      tmp = get(p, v, q, w);
      vec.push_back(make_pair(tmp, dcmp(cross(w, v)) > 0 ? -rate : rate));
    }
  }
}
long double solve(vector<complex<long double> > &g, complex<long double> s,
                  complex<long double> t) {
  int n = g.size();
  vec.clear();
  for (int i = 0; i < n; i++) {
    getSegmentInter(s, t - s, g[i], g[(i + 1) % n] - g[i]);
  }
  sort(vec.begin(), vec.end());
  long double sum = 0.0;
  long double cnt = 0.0;
  for (int i = 0; i < vec.size(); i++) {
    cnt += vec[i].second;
    if (dcmp(cnt)) sum += vec[i + 1].first - vec[i].first;
  }
  sum *= abs(t - s);
  return sum;
}
int main(void) {
  int n, m;
  scanf("%d%d", &n, &m);
  vector<complex<long double> > p;
  for (int i = 0; i < n; i++) {
    long double x, y;
    cin >> x >> y;
    p.push_back(complex<long double>(x, y));
  }
  for (int i = 0; i < m; i++) {
    long double sx, sy, tx, ty;
    cin >> sx >> sy >> tx >> ty;
    complex<long double> s = complex<long double>(sx, sy);
    complex<long double> t = complex<long double>(tx, ty);
    printf("%.10f\n", (double)solve(p, s, t));
  }
  return 0;
}
