#include <bits/stdc++.h>
using namespace std;
const long double EPS = is_floating_point<long double>() ? 1e-7 : 0;
int comp(long double a, long double b) {
  return (b - a > EPS) ? -1 : (a - b > EPS) ? 1 : 0;
}
struct point {
  long double x, y;
  point operator+(const point &q) const { return {x + q.x, y + q.y}; }
  point operator-(const point &q) const { return {x - q.x, y - q.y}; }
  long double operator%(const point &q) const { return x * q.y - y * q.x; }
  long double len2() const { return x * x + y * y; }
  double operator!() const { return sqrt(len2()); }
};
struct line {
  point a, ab;
  line() : a(), ab() {}
  line(const point &a, const point &b, bool points = true)
      : a(a), ab(points ? b - a : b) {}
  point b() const { return a + ab; }
};
double cutLength(const line &l, const vector<point> &p) {
  vector<pair<double, int>> inter;
  int n = p.size();
  for (int i = 0; i < n; i++) {
    line r(p[i], p[(i + 1) % n]);
    int f1 = comp((r.a - l.a) % l.ab, 0), f2 = comp((r.b() - l.a) % l.ab, 0);
    if (f1 == f2) {
      continue;
    }
    double tmp = ((r.a - l.a) % r.ab) / (l.ab % r.ab);
    inter.push_back({tmp, (f1 < f2 ? 1 : -1) * (f1 && f2 ? 2 : 1)});
  }
  sort(inter.begin(), inter.end());
  double ans = 0;
  for (int i = 0, j = 0; i + 1 < inter.size(); i++) {
    j += inter[i].second;
    ans += j ? inter[i + 1].first - inter[i].first : 0;
  }
  return !l.ab * ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  point a, b;
  cin >> n >> m;
  vector<point> p(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i].x >> p[i].y;
  }
  while (m--) {
    cin >> a.x >> a.y >> b.x >> b.y;
    cout << fixed << setprecision(10) << cutLength({a, b}, p) << '\n';
  }
  return 0;
}
