#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
const long double eps = 1e-8;
int n, m;
void read(long double &x) {
  double tmp;
  scanf("%lf", &tmp);
  x = tmp;
}
void print(long double x) { printf("%.10f\n", (double)x); }
struct P {
  long double x, y;
  long double length() { return sqrt(x * x + y * y); }
} a[N];
P operator-(const P &a, const P &b) { return {a.x - b.x, a.y - b.y}; }
long double cp(P a, P b) { return a.x * b.y - b.x * a.y; }
struct L {
  long double l;
  int v;
} stk[N];
int top;
bool cmp(L a, L b) { return a.l < b.l; }
long double Len(P a, P v, P p, P q) { return cp(p - a, q - p) / cp(v, q - p); }
int sign(long double x) {
  if (x < -eps) return -1;
  if (x > eps) return 1;
  return 0;
}
long double solve(P p, P v) {
  top = 0;
  for (int i = 1; i <= n; i++) {
    int s1 = sign(cp(v, a[i - 1] - p));
    int s2 = sign(cp(v, a[i] - p));
    if (s1 > 0 && s2 > 0 || s1 == 0 && s2 == 0 || s1 < 0 && s2 < 0) continue;
    if (s1 > s2)
      stk[++top] = {Len(p, v, a[i - 1], a[i]), (s1 != 0 && s2 != 0 ? 2 : 1)};
    else
      stk[++top] = {Len(p, v, a[i - 1], a[i]), (s1 != 0 && s2 != 0 ? -2 : -1)};
  }
  sort(stk + 1, stk + top + 1, cmp);
  long double ret = 0;
  int tmp = 0;
  for (int i = 1; i <= top; i++) {
    if (tmp) ret += stk[i].l - stk[i - 1].l;
    tmp += stk[i].v;
  }
  return ret * v.length();
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) read(a[i].x), read(a[i].y);
  a[0] = a[n];
  for (int j = 1; j <= m; j++) {
    P p, q;
    read(p.x);
    read(p.y);
    read(q.x);
    read(q.y);
    print(solve(p, q - p));
  }
}
