#include <bits/stdc++.h>
using namespace std;
template <class T>
T gcd(T a, T b) {
  return (b != 0 ? gcd<T>(b, a % b) : a);
}
template <class T>
T lcm(T a, T b) {
  return (a / gcd<T>(a, b) * b);
}
template <class T>
inline bool read(T &x) {
  int c = getchar();
  int sgn = 1;
  while (~c && c < '0' || c > '9') {
    if (c == '-') sgn = -1;
    c = getchar();
  }
  for (x = 0; ~c && '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0';
  x *= sgn;
  return ~c;
}
const int INF = 2000000009;
const int MX = 100005;
const long double EPS = 1e-9;
const int MOD = 1000000007;
struct PT {
  long double x, y;
  PT() {}
  PT(long double x, long double y) : x(x), y(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 double c) const { return PT(x * c, y * c); }
  PT operator/(long double c) const { return PT(x / c, y / c); }
};
long double cross(PT a, PT b) { return a.x * b.y - a.y * b.x; }
long double dot(PT a, PT b) { return a.x * b.x + a.y * b.y; }
long double area2(PT a, PT b, PT c) {
  return cross(a, b) + cross(b, c) + cross(c, a);
}
long double dist2(PT a, PT b) { return dot(b - a, b - a); }
inline int sign(long double a) {
  int ret = (a < -EPS) ? -1 : ((a > EPS) ? 1 : 0);
  return ret;
}
struct Line {
  PT v;
  long double a, b, c;
  Line() {}
  Line(PT p, PT q) : v(q - p), a(-v.y), b(v.x), c(cross(v, p)) {}
  long double side(PT p) { return cross(v, p) - c; }
};
long double commonLength(vector<PT> &p, PT a, PT b) {
  Line l = Line(a, b);
  long double ans = 0.0;
  int n = p.size();
  p.push_back(p[0]);
  vector<pair<long double, int> > vec;
  for (int i = 0; i < n; i++) {
    int s1 = sign(cross(b - a, p[i] - a));
    int s2 = sign(cross(b - a, p[i + 1] - a));
    if (s1 == s2) continue;
    Line t = Line(p[i], p[i + 1]);
    PT inter = (t.v * l.c - l.v * t.c) / cross(l.v, t.v);
    long double tmp = dot(inter, l.v);
    int f;
    if (s1 > s2)
      f = s1 && s2 ? 2 : 1;
    else
      f = s1 && s2 ? -2 : -1;
    vec.push_back(make_pair(tmp, f));
  }
  sort(vec.begin(), vec.end());
  for (int i = 0, j = 0; i + 1 < (int)vec.size(); i++) {
    j += vec[i].second;
    if (j) ans += vec[i + 1].first - vec[i].first;
  }
  ans = ans / sqrt(dot(l.v, l.v));
  p.pop_back();
  return ans;
}
int main() {
  int n = ({
    int a;
    read(a);
    a;
  }),
      m = ({
        int a;
        read(a);
        a;
      });
  vector<PT> pol;
  for (int i = 0; i < n; i++) {
    long double x = ({
      long double a;
      scanf("%Lf", &a);
      a;
    }),
                y = ({
                  long double a;
                  scanf("%Lf", &a);
                  a;
                });
    pol.push_back(PT(x, y));
  }
  while (m--) {
    PT a, b;
    a.x = ({
      long double a;
      scanf("%Lf", &a);
      a;
    }),
    a.y = ({
      long double a;
      scanf("%Lf", &a);
      a;
    }),
    b.x = ({
      long double a;
      scanf("%Lf", &a);
      a;
    }),
    b.y = ({
      long double a;
      scanf("%Lf", &a);
      a;
    });
    printf("%.8Lf\n", commonLength(pol, a, b));
  }
  return 0;
}
