#include <bits/stdc++.h>
const int maxn = 1003;
const double eps = 1e-5;
int n, m;
struct vec {
  double x, y;
  vec operator+(const vec &k) const { return {x + k.x, y + k.y}; }
  vec operator-(const vec &k) const { return {x - k.x, y - k.y}; }
  double cross(const vec &k) const { return x * k.y - y * k.x; }
  double len() const { return sqrt(x * x + y * y); }
} gon[maxn], p, q;
int sign(const double &u) { return u < -eps ? -1 : u > eps; }
double inter(const vec &a, const vec &u, const vec &b, const vec &v) {
  return ((a - b).cross(v)) / u.cross(v);
}
double solve(const vec &a, const vec &w) {
  static std::vector<std::pair<double, int> > it;
  it.clear();
  for (int i = 0, u, v; i < n; ++i) {
    u = sign(w.cross(gon[i] - a));
    v = sign(w.cross(gon[(i + 1) % n] - a));
    if (u == v) continue;
    it.push_back(std::make_pair(inter(a, w, gon[i], gon[(i + 1) % n] - gon[i]),
                                (u > v ? 1 : -1) * (u && v ? 2 : 1)));
  }
  std::sort(it.begin(), it.end());
  int inside = 0;
  double ans = 0;
  for (int i = 0; i + 1 < it.size(); ++i) {
    inside += it[i].second;
    if (inside) ans += it[i + 1].first - it[i].first;
  }
  return ans * w.len();
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) scanf("%lf%lf", &gon[i].x, &gon[i].y);
  for (int i = 0; i < m; ++i) {
    scanf("%lf%lf%lf%lf", &p.x, &p.y, &q.x, &q.y);
    printf("%.18lf\n", solve(p, q - p));
  }
  return 0;
}

### Prompt

Your task is to create a cpp solution to 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>
const int maxn = 1003;
const double eps = 1e-5;
int n, m;
struct vec {
  double x, y;
  vec operator+(const vec &k) const { return {x + k.x, y + k.y}; }
  vec operator-(const vec &k) const { return {x - k.x, y - k.y}; }
  double cross(const vec &k) const { return x * k.y - y * k.x; }
  double len() const { return sqrt(x * x + y * y); }
} gon[maxn], p, q;
int sign(const double &u) { return u < -eps ? -1 : u > eps; }
double inter(const vec &a, const vec &u, const vec &b, const vec &v) {
  return ((a - b).cross(v)) / u.cross(v);
}
double solve(const vec &a, const vec &w) {
  static std::vector<std::pair<double, int> > it;
  it.clear();
  for (int i = 0, u, v; i < n; ++i) {
    u = sign(w.cross(gon[i] - a));
    v = sign(w.cross(gon[(i + 1) % n] - a));
    if (u == v) continue;
    it.push_back(std::make_pair(inter(a, w, gon[i], gon[(i + 1) % n] - gon[i]),
                                (u > v ? 1 : -1) * (u && v ? 2 : 1)));
  }
  std::sort(it.begin(), it.end());
  int inside = 0;
  double ans = 0;
  for (int i = 0; i + 1 < it.size(); ++i) {
    inside += it[i].second;
    if (inside) ans += it[i + 1].first - it[i].first;
  }
  return ans * w.len();
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) scanf("%lf%lf", &gon[i].x, &gon[i].y);
  for (int i = 0; i < m; ++i) {
    scanf("%lf%lf%lf%lf", &p.x, &p.y, &q.x, &q.y);
    printf("%.18lf\n", solve(p, q - p));
  }
  return 0;
}

```