#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
struct Pt {
  double x, y;
  Pt() {}
  Pt(double x, double y) : x(x), y(y) {}
  Pt operator-(const Pt& a) { return Pt(x - a.x, y - a.y); }
};
vector<Pt> poly;
double cross(Pt a, Pt b) { return a.x * b.y - a.y * b.x; }
double sign(double a) {
  if (a < -EPS) return -1;
  return a > EPS;
}
double len(Pt& a) { return sqrt(a.x * a.x + a.y * a.y); }
double intersect(Pt a, Pt dira, Pt b, Pt dirb) {
  return cross((a - b), dirb) * len(dira) / cross(dira, dirb);
}
double solve(vector<Pt>& line, int n) {
  Pt dir = line[1] - line[0];
  vector<pair<double, double> > inters;
  for (int i = 0; i < (int)poly.size(); i++) {
    Pt u = poly[i] - line[0];
    Pt v = poly[(i + 1) % n] - line[0];
    double vu = cross(dir, u);
    double vv = cross(dir, v);
    if (sign(vu) == sign(vv)) continue;
    double inter =
        intersect(line[0], dir, poly[i], poly[(i + 1) % n] - poly[i]);
    int val = (vu > vv ? 1 : -1) * (vu && vv ? 2 : 1);
    inters.push_back({inter, val});
  }
  sort(inters.begin(), inters.end());
  double ans = 0;
  int inside = 0;
  for (int i = 0; i < (int)inters.size() - 1; i++) {
    inside += inters[i].second;
    if (inside) {
      ans += inters[i + 1].first - inters[i].first;
    }
  }
  return ans;
}
int main(void) {
  int n, m;
  double x, y;
  scanf(" %d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf(" %lf %lf", &x, &y);
    poly.push_back(Pt(x, y));
  }
  for (int i = 0; i < m; i++) {
    vector<Pt> line;
    for (int j = 0; j < 2; j++) {
      scanf(" %lf %lf", &x, &y);
      line.push_back(Pt(x, y));
    }
    double ans = solve(line, n);
    printf("%.12f\n", ans);
  }
  return 0;
}
