#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long INFF = 0x3f3f3f3f3f3f3f3fll;
const long long M = 1e9 + 7;
const long long maxn = 1007;
const double eps = 0.00000001;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
template <typename T>
inline T abs(T a) {
  return a > 0 ? a : -a;
}
template <typename T>
inline T powMM(T a, T b) {
  T ret = 1;
  for (; b; b >>= 1ll, a = a * a % M) ret = 1ll * ret * a % M;
  return ret;
}
int n, m, T;
int i, j, k, t;
int sgn(double x) {
  if (abs(x) < eps) return 0;
  if (x < 0) return -1;
  return 1;
}
struct point {
  double x, y;
  point(double _x = 0, double _y = 0) : x(_x), y(_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.y - y * b.x; }
  double len() { return sqrt(x * x + y * y); }
} a[maxn];
double len(point a, point b, point c, point d) {
  return (a - b).len() * ((c - a) ^ (d - c)) / ((b - a) ^ (d - c));
}
vector<pair<double, int> > dis;
int mark, mark1, mark2, sign;
double solve(point s, point e) {
  dis.clear();
  int i;
  for (i = 0; i < n; i++) {
    mark = sgn((e - s) ^ (a[i] - s)) - sgn((e - s) ^ (a[i + 1] - s));
    if (mark == 0) continue;
    dis.push_back(make_pair(len(s, e, a[i], a[i + 1]), mark));
  }
  sort(dis.begin(), dis.end());
  sign = 0;
  double ans = 0;
  for (i = 0; i < dis.size(); i++) {
    sign += dis[i].second;
    if (sign) ans += dis[i + 1].first - dis[i].first;
  }
  return ans;
}
point s, e;
int main() {
  scanf("%d%d", &n, &m);
  for (i = 0; i < n; i++) scanf("%lf%lf", &a[i].x, &a[i].y);
  a[n] = a[0];
  for (i = 0; i < m; i++) {
    scanf("%lf%lf%lf%lf", &s.x, &s.y, &e.x, &e.y);
    printf("%.15lf\n", solve(s, e));
  }
}
