#include <bits/stdc++.h>
using namespace std;
inline int icmp(long long x) { return (x > 0) - (x < 0); }
int get() {
  double x;
  scanf("%lf", &x);
  if (x < 0) return x * 100 - 1e-6;
  return x * 100 + 1e-6;
}
struct P {
  int x, y;
  P() {}
  P(int x, int y) : x(x), y(y) {}
  void read() { x = get(), y = get(); }
  void print() const { printf("%d %d\n", x, y); }
};
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 long Cross(const P &A, const P &B) {
  return 1LL * A.x * B.y - 1LL * A.y * B.x;
}
long long Dot(const P &A, const P &B) {
  return 1LL * A.x * B.x + 1LL * A.y * B.y;
}
double Length(const P &A) { return sqrt(Dot(A, A)); }
const int maxn = 1001;
int n, m;
P p[maxn];
vector<pair<double, int> > vec;
double getPos(const P &p, const P &v, const P &q, const P &w) {
  P u = p - q;
  return 1. * Cross(w, u) / Cross(v, w);
}
void getSegmentInter(const P &p, const P &v, const P &q, const P &w) {
  int f1 = icmp(Cross(v, q - p));
  int f2 = icmp(Cross(v, q + w - p));
  if (!f1 && !f2) return;
  int rate = !f1 || !f2 ? 1 : 2;
  if (f1 > f2) vec.push_back(make_pair(getPos(p, v, q, w), +rate));
  if (f1 < f2) vec.push_back(make_pair(getPos(p, v, q, w), -rate));
}
int main() {
  while (~scanf("%d %d", &n, &m) && 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());
      double ans = 0;
      int k = 0;
      for (size_t i = 0; i < vec.size(); i++) {
        k += vec[i].second;
        if (k) {
          while (i == vec.size() - 1)
            ;
          ans += vec[i + 1].first - vec[i].first;
        }
      }
      ans *= Length(b - a);
      printf("%.20f\n", double(ans) / 100);
    }
  }
}
