#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-6;
pair<double, double> point[1005];
pair<double, double> line[2];
int n, m;
double det(pair<double, double> a, pair<double, double> b,
           pair<double, double> c, pair<double, double> d) {
  return (b.first - a.first) * (d.second - c.second) -
         (b.second - a.second) * (d.first - c.first);
}
int judge(pair<double, double> a, pair<double, double> b,
          pair<double, double> c) {
  if ((det(a, b, a, c) > eps)) return 1;
  if ((det(a, b, a, c) < -eps)) return -1;
  return 0;
}
double dingdianbifen(pair<double, double> a, pair<double, double> b,
                     pair<double, double> c, pair<double, double> d) {
  return (det(a, c, c, d) / det(a, b, c, d));
}
double solve(pair<double, double> a, pair<double, double> b) {
  vector<pair<double, int> > p;
  for (int i = 0; i < n; i++) {
    int nxt = (i + 1) % n;
    if (judge(a, b, point[i]) == judge(a, b, point[nxt])) continue;
    p.push_back(make_pair(dingdianbifen(a, b, point[i], point[nxt]),
                          judge(a, b, point[i]) - judge(a, b, point[nxt])));
  }
  sort(p.begin(), p.end(), less<pair<double, int> >());
  int sign = 0;
  double ans = 0;
  for (int i = 0; i < (int)p.size() - 1; i++) {
    sign += p[i].second;
    if (sign != 0) {
      ans += p[i + 1].first - p[i].first;
    }
  }
  return fabs(ans * sqrt((a.first - b.first) * (a.first - b.first) +
                         (a.second - b.second) * (a.second - b.second)));
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    scanf("%lf%lf", &point[i].first, &point[i].second);
  }
  for (int i = 0; i < m; i++) {
    scanf("%lf%lf%lf%lf", &line[0].first, &line[0].second, &line[1].first,
          &line[1].second);
    printf("%.10f\n", solve(line[0], line[1]));
  }
  return 0;
}
