#include <bits/stdc++.h>
using namespace std;
const int mn = 1e3 + 5;
int n, m;
namespace Geometry {
const double eps = 1e-10;
double add(double a, double b) {
  if (fabs(a + b) < eps * (fabs(a) + fabs(b))) return 0;
  return a + b;
}
struct point {
  double x, y;
  point() {}
  point(double x, double y) : x(x), y(y) {}
  point operator+(point p) { return point(add(x, p.x), add(y, p.y)); }
  point operator-(point p) { return point(add(x, -p.x), add(y, -p.y)); }
  point operator*(double d) { return point(x * d, y * d); }
  double dot(point p) { return add(x * p.x, y * p.y); }
  double det(point p) { return add(x * p.y, -y * p.x); }
} p[mn];
bool on_seg(point p1, point p2, point q) {
  return (p1 - q).det(p2 - q) == 0 && (p1 - q).dot(p2 - q) <= 0;
}
point intersection(point p1, point p2, point q1, point q2) {
  return p1 + (p2 - p1) * ((q2 - q1).det(q1 - p1) / (q2 - q1).det(p2 - p1));
}
double PositionofIntersection(point p1, point p2, point q1, point q2) {
  return ((q2 - q1).det(q1 - p1) / (q2 - q1).det(p2 - p1));
}
bool Parallel(point p1, point p2, point q1, point q2) {
  return (p1 - p2).det(q1 - q2) == 0;
}
}  // namespace Geometry
using namespace Geometry;
int sgn(double p) {
  if (p < 0) return -1;
  return p > 0;
}
double solve(point a, point b) {
  vector<pair<double, int> > v;
  for (int i = 1; i <= n; i++) {
    int k1 = sgn(b.det(p[i] - a));
    int k2 = sgn(b.det(p[i + 1] - a));
    if (k1 == k2) continue;
    double u = PositionofIntersection(a, a + b, p[i], p[i + 1]);
    int w = sgn((p[i + 1] - p[i]).det(b));
    if (abs(k1) && k2 == 0) {
      v.push_back(make_pair(u, w));
    } else if (abs(k2) && k1 == 0) {
      v.push_back(make_pair(u, w));
    } else {
      v.push_back(make_pair(u, 2 * w));
    }
  }
  sort(v.begin(), v.end());
  double ans = 0;
  int sign = 0;
  for (int i = 0; i < (int)v.size() - 1; i++) {
    sign += v[i].second;
    if (sign) ans += v[i + 1].first - v[i].first;
  }
  return ans * hypot(b.x, b.y);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%lf%lf", &p[i].x, &p[i].y);
  p[n + 1] = p[1];
  while (m--) {
    point a, b;
    scanf("%lf%lf%lf%lf", &a.x, &a.y, &b.x, &b.y);
    printf("%.20f\n", solve(a, b - a));
  }
  return 0;
}

### Prompt

In CPP, your task is to solve the following problem:

Given simple (without self-intersections) n-gon. It is not necessary convex. Also you are given m lines. For each line find the length of common part of the line and the n-gon.

The boundary of n-gon belongs to polygon. It is possible that n-gon contains 180-degree angles.

Input

The first line contains integers n and m (3 ≤ n ≤ 1000;1 ≤ m ≤ 100). The following n lines contain coordinates of polygon vertices (in clockwise or counterclockwise direction). All vertices are distinct.

The following m lines contain line descriptions. Each of them contains two distict points of a line by their coordinates.

All given in the input coordinates are real numbers, given with at most two digits after decimal point. They do not exceed 105 by absolute values.

Output

Print m lines, the i-th line should contain the length of common part of the given n-gon and the i-th line. The answer will be considered correct if the absolute or relative error doesn't exceed 10 - 6.

Examples

Input

4 3
0 0
1 0
1 1
0 1
0 0 1 1
0 0 0 1
0 0 1 -1


Output

1.41421356237309514547
1.00000000000000000000
0.00000000000000000000

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int mn = 1e3 + 5;
int n, m;
namespace Geometry {
const double eps = 1e-10;
double add(double a, double b) {
  if (fabs(a + b) < eps * (fabs(a) + fabs(b))) return 0;
  return a + b;
}
struct point {
  double x, y;
  point() {}
  point(double x, double y) : x(x), y(y) {}
  point operator+(point p) { return point(add(x, p.x), add(y, p.y)); }
  point operator-(point p) { return point(add(x, -p.x), add(y, -p.y)); }
  point operator*(double d) { return point(x * d, y * d); }
  double dot(point p) { return add(x * p.x, y * p.y); }
  double det(point p) { return add(x * p.y, -y * p.x); }
} p[mn];
bool on_seg(point p1, point p2, point q) {
  return (p1 - q).det(p2 - q) == 0 && (p1 - q).dot(p2 - q) <= 0;
}
point intersection(point p1, point p2, point q1, point q2) {
  return p1 + (p2 - p1) * ((q2 - q1).det(q1 - p1) / (q2 - q1).det(p2 - p1));
}
double PositionofIntersection(point p1, point p2, point q1, point q2) {
  return ((q2 - q1).det(q1 - p1) / (q2 - q1).det(p2 - p1));
}
bool Parallel(point p1, point p2, point q1, point q2) {
  return (p1 - p2).det(q1 - q2) == 0;
}
}  // namespace Geometry
using namespace Geometry;
int sgn(double p) {
  if (p < 0) return -1;
  return p > 0;
}
double solve(point a, point b) {
  vector<pair<double, int> > v;
  for (int i = 1; i <= n; i++) {
    int k1 = sgn(b.det(p[i] - a));
    int k2 = sgn(b.det(p[i + 1] - a));
    if (k1 == k2) continue;
    double u = PositionofIntersection(a, a + b, p[i], p[i + 1]);
    int w = sgn((p[i + 1] - p[i]).det(b));
    if (abs(k1) && k2 == 0) {
      v.push_back(make_pair(u, w));
    } else if (abs(k2) && k1 == 0) {
      v.push_back(make_pair(u, w));
    } else {
      v.push_back(make_pair(u, 2 * w));
    }
  }
  sort(v.begin(), v.end());
  double ans = 0;
  int sign = 0;
  for (int i = 0; i < (int)v.size() - 1; i++) {
    sign += v[i].second;
    if (sign) ans += v[i + 1].first - v[i].first;
  }
  return ans * hypot(b.x, b.y);
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%lf%lf", &p[i].x, &p[i].y);
  p[n + 1] = p[1];
  while (m--) {
    point a, b;
    scanf("%lf%lf%lf%lf", &a.x, &a.y, &b.x, &b.y);
    printf("%.20f\n", solve(a, b - a));
  }
  return 0;
}

```