#include <bits/stdc++.h>
using namespace std;
struct PT {
  long long x, y;
  PT() {}
  PT(long long x, long long y) : x(x), y(y) {}
  PT(const PT &p) : x(p.x), y(p.y) {}
  PT operator+(const PT &p) const { return PT(x + p.x, y + p.y); }
  PT operator-(const PT &p) const { return PT(x - p.x, y - p.y); }
  PT operator*(long long c) const { return PT(x * c, y * c); }
  PT operator/(long long c) const { return PT(x / c, y / c); }
};
long long dot(PT p, PT q) { return p.x * q.x + p.y * q.y; }
long long dist2(PT p, PT q) { return dot(p - q, p - q); }
long long cross(PT p, PT q) { return p.x * q.y - p.y * q.x; }
long long cross(PT a, PT b, PT c) { return cross(b - a, c - a); }
ostream &operator<<(ostream &os, const PT &p) {
  return os << "(" << p.x << "," << p.y << ")";
}
struct Fraction {
  long long a, b;
  Fraction(long long _a, long long _b) {
    a = _a;
    b = _b;
    assert(b);
    if (b < 0) b = -b, a = -a;
  }
  bool operator<(const Fraction &o) const {
    return 1.0L * a * o.b < 1.0L * b * o.a;
  }
};
Fraction distance(PT O, PT X, PT A, PT B) {
  B = B - A;
  A = A - O;
  X = X - O;
  assert(cross(X, B));
  return Fraction(cross(A, B), cross(X, B));
}
int sign(long long a) { return a == 0 ? 0 : (a > 0 ? 1 : -1); }
const int PR = 100;
PT inputPoint() {
  double x, y;
  cin >> x >> y;
  return PT(round(PR * x), round(PR * y));
}
double LinePolygonIntersection(PT A, PT B, const vector<PT> &p) {
  int n = p.size();
  vector<pair<Fraction, int>> special;
  for (int i = 0; i < n; i++) {
    PT X = p[i], Y = p[(i + 1) % n], W = p[(i - 1 + n) % n];
    long long crx = cross(A, B, X), cry = cross(A, B, Y), crw = cross(A, B, W);
    if (crx == 0) {
      Fraction f(0, 1);
      if (B.x != A.x)
        f = Fraction(X.x - A.x, B.x - A.x);
      else
        f = Fraction(X.y - A.y, B.y - A.y);
      if (sign(crw) && sign(cry)) {
        if (sign(crw) != sign(cry)) special.push_back({f, 0});
      } else if (sign(cry))
        special.push_back({f, sign(cry)});
      else if (sign(crw))
        special.push_back({f, sign(crw)});
    } else if (sign(crx) == -sign(cry)) {
      Fraction f = distance(A, B, X, Y);
      special.push_back({f, 0});
    }
  }
  sort(special.begin(), special.end());
  bool active = false;
  int sgn = 0;
  double prv = 0, ans = 0;
  for (auto &pr : special) {
    double d = 1.0 * pr.first.a / pr.first.b;
    int tp = pr.second;
    if (sgn) {
      assert(sgn && tp);
      if (sgn != tp) active = !active;
      ans += d - prv;
      sgn = 0;
    } else {
      if (active) ans += d - prv;
      if (tp == 0)
        active = !active;
      else
        sgn = tp;
    }
    prv = d;
  }
  return ans * sqrt(dot(B - A, B - A));
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<PT> p(n);
  for (int i = 0; i < n; i++) p[i] = inputPoint();
  while (m--) {
    PT A, B;
    A = inputPoint();
    B = inputPoint();
    cout << setprecision(15) << fixed << LinePolygonIntersection(A, B, p) / PR
         << endl;
  }
}
