#include <bits/stdc++.h>
using namespace std;
int n, m, ch[1005], tt;
long long x[1005], y[1005], xa, ya, xb, yb, xc[1005], yc[1005], cc[1005], xcc,
    ycc, ccc, tx, ty, ca, cb, tc;
double ta, tb, ans, taa, tbb;
pair<double, double> pt;
vector<pair<pair<pair<double, double>, pair<double, double> >, int> > xx;
bool ins;
double dist(pair<double, double>& a, pair<double, double>& b) {
  ta = a.first - b.first;
  tb = a.second - b.second;
  return sqrt(ta * ta + tb * tb);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int a = 0; a < n; a++) {
    scanf("%lf%lf", &ta, &tb);
    x[a] = (long long)round(ta * 200);
    y[a] = (long long)round(tb * 200);
  }
  for (int a = 0; a < m; a++) {
    ans = 0;
    xx.clear();
    scanf("%lf%lf", &ta, &tb);
    xa = (long long)round(ta * 200);
    ya = (long long)round(tb * 200);
    scanf("%lf%lf", &ta, &tb);
    xb = (long long)round(ta * 200);
    yb = (long long)round(tb * 200);
    xcc = ya - yb;
    ycc = xb - xa;
    ccc = xa * xcc + ya * ycc;
    for (int b = 0; b < n; b++) {
      xc[b] = y[b] - y[(b + 1) % n];
      yc[b] = x[(b + 1) % n] - x[b];
      cc[b] = x[b] * xc[b] + y[b] * yc[b];
      tx = (x[b] + x[(b + 1) % n]) / 2;
      ty = (y[b] + y[(b + 1) % n]) / 2;
      if (tx * xcc + ty * ycc == ccc)
        ch[b] = 0;
      else if (tx * xcc + ty * ycc > ccc)
        ch[b] = 1;
      else
        ch[b] = -1;
    }
    for (int b = 0; b < n; b++) {
      if (ch[b] == 0 && xc[b] * ycc == yc[b] * xcc)
        ans += sqrt((double)(xc[b] * xc[b] + yc[b] * yc[b]));
      else if (xcc * x[(b + 1) % n] + ycc * y[(b + 1) % n] != ccc) {
        if (xcc * x[b] + ycc * y[b] == ccc) {
          if (-ch[b] == ch[(b + n - 1) % n])
            xx.push_back(make_pair(
                make_pair(make_pair(x[b], y[b]), make_pair(0, 0)), 0));
          else if (ch[(b + n - 1) % n] == 0) {
            tt = (b + n - 1) % n;
            while (ch[tt] == 0) tt = (tt + n - 1) % n;
            if (ch[tt] == ch[b])
              xx.push_back(make_pair(
                  make_pair(make_pair(x[b], y[b]),
                            make_pair(x[(tt + 1) % n], y[(tt + 1) % n])),
                  1));
            else
              xx.push_back(make_pair(
                  make_pair(make_pair(x[b], y[b]),
                            make_pair(x[(tt + 1) % n], y[(tt + 1) % n])),
                  2));
            if (xx.back().first.first > xx.back().first.second)
              swap(xx.back().first.first, xx.back().first.second);
          }
        } else if ((xcc * x[b] + ycc * y[b] > ccc) ^
                   (xcc * x[(b + 1) % n] + ycc * y[(b + 1) % n] > ccc)) {
          ca = cc[b] / (1LL << 30);
          cb = ccc / (1LL << 30);
          ta = ca * ycc - yc[b] * cb;
          tb = xc[b] * cb - ca * xcc;
          ca = cc[b] - ca * (1LL << 30);
          cb = ccc - cb * (1LL << 30);
          taa = ca * ycc - yc[b] * cb;
          tbb = xc[b] * cb - ca * xcc;
          tc = xc[b] * ycc - yc[b] * xcc;
          xx.push_back(
              make_pair(make_pair(make_pair(ta / tc * (1LL << 30) + taa / tc,
                                            tb / tc * (1LL << 30) + tbb / tc),
                                  make_pair(0, 0)),
                        0));
        }
      }
    }
    sort(xx.begin(), xx.end());
    for (int b = 0; b < xx.size(); b++) {
      if (xx[b].second == 1) {
        if (ins) {
          ans += dist(pt, xx[b].first.first);
          pt = xx[b].first.second;
        }
      } else if (xx[b].second == 2) {
        if (ins)
          ans += dist(pt, xx[b].first.first);
        else
          pt = xx[b].first.second;
        ins = !ins;
      } else {
        if (ins) ans += dist(pt, xx[b].first.first);
        ins = !ins;
        pt = xx[b].first.first;
      }
    }
    printf("%.30lf\n", ans / 200);
  }
  return 0;
}
