#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
int dcmp(long double x) { return x < -eps ? -1 : x > eps; }
struct Point {
  long double x, y;
  Point() {}
  Point(long double xx, long double yy) : x(xx), y(yy) {}
  Point operator-(const Point &b) const { return Point(x - b.x, y - b.y); }
  long double operator^(const Point &b) const { return x * b.y - y * b.x; }
  long double operator*(const Point &b) const { return x * b.x + y * b.y; }
} poly[1010];
void work(Point p, Point v, int n) {
  vector<pair<long double, int> > node;
  for (int i = 1; i <= n; ++i) {
    Point u = poly[i + 1] - poly[i];
    int s1 = dcmp(v ^ (poly[i] - p));
    int s2 = dcmp(v ^ (poly[i + 1] - p));
    if (s1 == s2) continue;
    long double rate = (u ^ (p - poly[i])) / (v ^ u);
    if (s1 > s2)
      node.emplace_back(rate, s1 && s2 ? 2 : 1);
    else
      node.emplace_back(make_pair(rate, s1 && s2 ? -2 : -1));
  }
  sort(node.begin(), node.end());
  int flag = 0;
  long double ans = 0;
  int _size = node.size();
  for (int i = 0; i < _size - 1; ++i) {
    flag += node[i].second;
    if (flag) ans += (node[i + 1].first - node[i].first);
  }
  cout << fixed << setprecision(6) << ans * (sqrt(v.x * v.x + v.y * v.y))
       << endl;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) cin >> poly[i].x >> poly[i].y;
  poly[n + 1] = poly[1];
  for (int i = 1; i <= m; ++i) {
    Point A, B;
    cin >> A.x >> A.y >> B.x >> B.y;
    work(A, B - A, n);
  }
  return 0;
}
