#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-8;
const int M = 1005;
inline int sgn(double x) { return (x > EPS) - (x < -EPS); }
struct Point {
  double x, y;
  Point(double x = 0, double y = 0) : x(x), y(y) {}
  void read() { scanf("%lf%lf", &x, &y); }
  Point operator+(const Point &B) const { return Point(x + B.x, y + B.y); }
  Point operator-(const Point &B) const { return Point(x - B.x, y - B.y); }
  double operator*(const Point &B) const { return x * B.x + y * B.y; }
  double operator^(const Point &B) const { return x * B.y - y * B.x; }
  double len() { return sqrt(x * x + y * y); }
  bool operator<(const Point &B) const {
    return sgn(x - B.x) == 0 ? sgn(y - B.y) < 0 : sgn(x - B.x) < 0;
  }
};
Point p[M];
int n, q;
Point A, B;
double solve() {
  vector<pair<double, int>> l;
  for (int i = 0; i < n; i++) {
    int f1 = sgn((p[i] - A) ^ (B - A));
    int f2 = sgn((p[i + 1] - A) ^ (B - A));
    if (f1 == f2) continue;
    double ar1 = (p[i] - A) ^ (p[i + 1] - p[i]);
    double ar2 = (B - A) ^ (p[i + 1] - p[i]);
    l.emplace_back(ar1 / ar2, f2 - f1);
  }
  sort(l.begin(), l.end());
  int cnt = 0;
  double sum = 0, lst = 0;
  for (auto pa : l) {
    if (cnt > 0) sum += pa.first - lst;
    lst = pa.first;
    cnt += pa.second;
  }
  return sum * ((B - A).len());
}
int main() {
  scanf("%d%d", &n, &q);
  for (int i = 0; i < n; i++) p[i].read();
  p[n] = p[0];
  double area = 0;
  for (int i = 0; i < n; i++) area += p[i] ^ p[i + 1];
  if (area < 0) reverse(p, p + n + 1);
  while (q--) {
    A.read();
    B.read();
    printf("%.8f\n", solve());
  }
  return 0;
}
