#include <bits/stdc++.h>
using namespace std;
struct pt {
  double x;
  double y;
};
pt operator+(const pt &a, const pt &b) { return {a.x + b.x, a.y + b.y}; }
pt operator-(const pt &a, const pt &b) { return {a.x - b.x, a.y - b.y}; }
inline double cross(pt a, pt b) { return a.x * b.y - a.y * b.x; }
struct linePart {
  double length;
  int flag;
};
bool linePartsSorter(const linePart &a, const linePart &b) {
  return a.length < b.length;
}
inline void getLineParts(vector<linePart> &l, pt linePt, pt lineVec, pt segPt,
                         pt segVec) {
  if (cross(lineVec, segVec) != 0) {
    double cross1 = cross(lineVec, segPt - linePt);
    double cross2 = cross(lineVec, segPt + segVec - linePt);
    if (cross1 > cross2) {
      swap(cross1, cross2);
    }
    if (cross1 <= 0 && cross2 >= 0) {
      int flag;
      if (cross1 == 0 || cross2 == 0) {
        flag = 1;
      } else {
        flag = 2;
      }
      double length = cross(linePt - segPt, segVec) / cross(lineVec, segVec);
      l.push_back({length, cross(lineVec, segVec) > 0 ? flag : -flag});
    }
  }
}
inline double distance(pt a, pt b) {
  pt c = b - a;
  return sqrt(c.x * c.x + c.y * c.y);
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<pt> vertices(n + 1);
  for (int i = 0; i < n; i++) {
    double x, y;
    cin >> x >> y;
    vertices[i].x = x;
    vertices[i].y = y;
  }
  vertices[n] = vertices[0];
  while (m--) {
    pt a, b;
    double x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    a.x = x1;
    a.y = y1;
    b.x = x2;
    b.y = y2;
    vector<linePart> l;
    for (int i = 0; i < n; i++) {
      getLineParts(l, a, b - a, vertices[i], vertices[i + 1] - vertices[i]);
    }
    sort(l.begin(), l.end(), linePartsSorter);
    double percentage = 0;
    int insideFlag = 0;
    for (size_t i = 0; i < l.size(); i++) {
      insideFlag += l[i].flag;
      if (insideFlag != 0) {
        percentage += l[i + 1].length - l[i].length;
      }
    }
    cout << fixed << setprecision(7) << percentage * distance(a, b) << '\n';
  }
}
