#include <bits/stdc++.h>
using namespace std;
long double get() {
  double x;
  scanf("%lf", &x);
  return x;
}
struct P {
  long double x, y;
  P() {}
  P(long double x, long double y) : x(x), y(y) {}
  void read() { x = get(), y = get(); }
};
P operator+(const P &A, const P &B) { return P(A.x + B.x, A.y + B.y); }
P operator-(const P &A, const P &B) { return P(A.x - B.x, A.y - B.y); }
long double Cross(const P &A, const P &B) { return A.x * B.y - A.y * B.x; }
long double Dot(const P &A, const P &B) { return A.x * B.x + A.y * B.y; }
long double Length(const P &A) { return sqrt(Dot(A, A)); }
const long double eps = 1e-10;
inline int dcmp(long double x) { return (x > eps) - (x < -eps); }
const int maxn = 1001;
int n, m;
P p[maxn];
vector<pair<long double, long double> > vec;
inline void add(long double x, long double flag) {
  vec.push_back(make_pair(x, -flag));
}
long double get(const P &p, const P &v, const P &q, const P &w) {
  P u = p - q;
  return Cross(w, u) / Cross(v, w);
}
void getSegmentInter(const P &p, const P &v, const P &q, const P &w) {
  if (!dcmp(Cross(v, w))) {
    if (!dcmp(Cross(v, q - p))) {
      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);
      add(l, +1);
      add(r, -1);
    }
  } else {
    static 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) rate = .5;
      tmp = get(p, v, q, w);
      add(tmp, dcmp(Cross(w, v)) > 0 ? +rate : -rate);
    }
  }
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) p[i].read();
  p[n] = p[0];
  while (m--) {
    P a, b;
    a.read();
    b.read();
    vec.clear();
    for (int i = 0; i < n; i++)
      getSegmentInter(a, b - a, p[i], p[i + 1] - p[i]);
    sort(vec.begin(), vec.end());
    long double ans = 0, k = 0;
    for (size_t i = 0; i < vec.size(); i++) {
      k += vec[i].second;
      if (dcmp(k)) ans += vec[i + 1].first - vec[i].first;
    }
    ans *= Length(b - a);
    printf("%.20f\n", double(ans));
  }
}
