#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:255000000")
bool firstout = 1;
template <class T>
T &minn(T &a, T b) {
  if (b < a) a = b;
  return a;
}
template <class T>
T &maxx(T &a, T b) {
  if (a < b) a = b;
  return a;
}
int &madd(int &a, int b) {
  a += b;
  if (a >= 1000000007) a -= 1000000007;
  return a;
}
int &msub(int &a, int b) {
  a -= b;
  if (a < 0) a += 1000000007;
  return a;
}
int &mmult(int &a, int b) { return a = (long long)a * b % 1000000007; }
int mdiv(long long a, long long b, long long m) {
  a = (a % m + m) % m;
  b = (b % m + m) % m;
  if (a % b == 0) return a / b;
  return (a + m * mdiv(-a, m, b)) / b;
}
int n, m, q, na, nr;
double X[2 * 1012], Y[2 * 1012];
pair<double, double> A[2 * 1012];
pair<pair<double, double>, int> R[2 * 1012];
int main() {
  int i, j, k;
  char c;
  int a, d;
  int ts;
  for (ts = 1; scanf("%"
                     "d",
                     &(n)) > 0;
       ++ts) {
    scanf(
        "%"
        "d",
        &(q));
    for (i = (0); i < (n); ++i)
      scanf(
          "%"
          "lf",
          &(X[i])),
          scanf(
              "%"
              "lf",
              &(Y[i]));
    for (i = (n); i < (n + n); ++i) X[i] = X[i - n], Y[i] = Y[i - n];
    for (; q--;) {
      na = nr = 0;
      double x, y, xx, yy, a, b, c;
      scanf(
          "%"
          "lf",
          &(x));
      scanf(
          "%"
          "lf",
          &(y));
      scanf(
          "%"
          "lf",
          &(xx));
      scanf(
          "%"
          "lf",
          &(yy));
      a = yy - y;
      b = x - xx;
      c = +y * xx - x * yy;
      for (i = (0); i < (n); ++i) {
        double p1 = a * X[i] + b * Y[i] + c;
        double p2 = a * X[i + 1] + b * Y[i + 1] + c;
        if (p1 * p2 < 0 && fabs(p1) > 1e-9 && fabs(p2) > 1e-9) {
          x = X[i + 1] - X[i];
          y = Y[i + 1] - Y[i];
          p1 = fabs(p1);
          p2 = fabs(p2);
          p1 /= p1 + p2;
          A[na++] = make_pair(X[i] + p1 * x, Y[i] + p1 * y);
        }
        if (fabs(p1) < 1e-9 && fabs(p2) < 1e-9) {
          pair<double, double> r1, r2;
          r1 = make_pair(X[i], Y[i]);
          r2 = make_pair(X[i + 1], Y[i + 1]);
          if (r2 < r1) swap(r1, r2);
          R[nr++] = make_pair(r1, 1);
          R[nr++] = make_pair(r2, -1);
        }
      }
      for (i = (1); i < (n + 1); ++i) {
        if (fabs(a * X[i - 1] + b * Y[i - 1] + c) > 1e-9 &&
            fabs(a * X[i] + b * Y[i] + c) <= 1e-9) {
          double p1 = a * X[i - 1] + b * Y[i - 1] + c;
          for (j = i + 1; fabs(a * X[j] + b * Y[j] + c) <= 1e-9; ++j)
            ;
          double p2 = a * X[j] + b * Y[j] + c;
          if (p1 * p2 < 0 && fabs(p1) > 1e-9 && fabs(p2) > 1e-9) {
            A[na++] = make_pair(X[i], Y[i]);
          }
        }
      }
      sort(A, A + na);
      for (i = (0); i < (na); i += (2)) {
        R[nr++] = make_pair(A[i], 1);
        R[nr++] = make_pair(A[i + 1], -1);
      }
      sort(R, R + nr);
      double res = 0.;
      int r = 0;
      for (i = (0); i < (nr); ++i) {
        if (r > 0)
          res += hypot(R[i].first.first - R[i - 1].first.first,
                       R[i].first.second - R[i - 1].first.second);
        r += R[i].second;
      }
      printf(
          "%"
          ".7lf",
          (res));
      printf("\n"), firstout = 1;
    }
  }
  return 0;
}
