#include <bits/stdc++.h>
using namespace std;
const long double eps = 1e-14;
inline int dcmp(long double x) { return (x > eps) - (x < -eps); }
class vec {
 public:
  long double x, y;
  vec() { x = 0, y = 0; }
  vec(long double tx, long double ty) { x = tx, y = ty; }
  vec operator-(const vec &t) const { return vec(x - t.x, y - t.y); }
  vec operator+(const vec &t) const { return vec(x + t.x, y + t.y); }
  long double operator*(const vec &t) const { return x * t.y - y * t.x; }
  long double operator^(const vec &t) const { return x * t.x + y * t.y; }
  vec operator*(const double &t) const { return vec(x * t, y * t); }
  long double len() { return sqrt(x * x + y * y); }
  long double len2() { return x * x + y * y; }
  bool operator<(const vec &t) const {
    if (x == t.x) return y < t.y;
    return x < t.x;
  }
} p[1010];
vector<pair<long double, long double> > v;
inline long double getp(vec x, vec A, vec B) {
  vec t = (B - A);
  return (t ^ x) / (t ^ t);
}
void intersect(vec ss, vec st, vec la, vec lb) {
  vec ds = st - ss, dl = lb - la;
  if (dcmp(ds * dl) == 0) {
    if (dcmp(ds * (la - ss)) == 0) {
      long double l = getp(ss, la, lb), r = getp(st, la, lb);
      if (l > r) swap(l, r);
      v.push_back(make_pair(l, 1));
      v.push_back(make_pair(r, -1));
    }
  }
  if (dcmp(dl * (ss - la)) == dcmp(dl * (st - la))) return;
  long double d1 = abs((ss - la) * dl), d2 = abs((st - la) * dl);
  if (dcmp(d1) == 0) {
    long double p = getp(ss, la, lb);
    if (dcmp(ds * dl) > 0)
      v.push_back(make_pair(p, 0.5));
    else
      v.push_back(make_pair(p, -0.5));
  } else if (dcmp(d2) == 0) {
    long double p = getp(st, la, lb);
    if (dcmp(ds * dl) > 0)
      v.push_back(make_pair(p, 0.5));
    else
      v.push_back(make_pair(p, -0.5));
  } else {
    vec c = ss + ds * (d1 / (d1 + d2));
    long double p = getp(c, la, lb);
    if (dcmp(ds * dl) > 0)
      v.push_back(make_pair(p, 1));
    else
      v.push_back(make_pair(p, -1));
  }
}
int n, m;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%Lf%Lf", &p[i].x, &p[i].y);
  p[n] = p[0];
  for (int i = 1; i <= m; i++) {
    vec A, B;
    scanf("%Lf%Lf%Lf%Lf", &A.x, &A.y, &B.x, &B.y);
    long double ans = 0;
    v.clear();
    for (int j = 0; j < n; j++) intersect(p[j], p[j + 1], A, B);
    sort(v.begin(), v.end());
    double r = 0;
    for (int j = 0; j < v.size(); j++) {
      r += v[j].second;
      if (dcmp(r)) ans += v[j + 1].first - v[j].first;
    }
    ans *= (B - A).len();
    printf("%.13Lf\n", ans);
  }
  return 0;
}

### Prompt

Create a solution in CPP for 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 long double eps = 1e-14;
inline int dcmp(long double x) { return (x > eps) - (x < -eps); }
class vec {
 public:
  long double x, y;
  vec() { x = 0, y = 0; }
  vec(long double tx, long double ty) { x = tx, y = ty; }
  vec operator-(const vec &t) const { return vec(x - t.x, y - t.y); }
  vec operator+(const vec &t) const { return vec(x + t.x, y + t.y); }
  long double operator*(const vec &t) const { return x * t.y - y * t.x; }
  long double operator^(const vec &t) const { return x * t.x + y * t.y; }
  vec operator*(const double &t) const { return vec(x * t, y * t); }
  long double len() { return sqrt(x * x + y * y); }
  long double len2() { return x * x + y * y; }
  bool operator<(const vec &t) const {
    if (x == t.x) return y < t.y;
    return x < t.x;
  }
} p[1010];
vector<pair<long double, long double> > v;
inline long double getp(vec x, vec A, vec B) {
  vec t = (B - A);
  return (t ^ x) / (t ^ t);
}
void intersect(vec ss, vec st, vec la, vec lb) {
  vec ds = st - ss, dl = lb - la;
  if (dcmp(ds * dl) == 0) {
    if (dcmp(ds * (la - ss)) == 0) {
      long double l = getp(ss, la, lb), r = getp(st, la, lb);
      if (l > r) swap(l, r);
      v.push_back(make_pair(l, 1));
      v.push_back(make_pair(r, -1));
    }
  }
  if (dcmp(dl * (ss - la)) == dcmp(dl * (st - la))) return;
  long double d1 = abs((ss - la) * dl), d2 = abs((st - la) * dl);
  if (dcmp(d1) == 0) {
    long double p = getp(ss, la, lb);
    if (dcmp(ds * dl) > 0)
      v.push_back(make_pair(p, 0.5));
    else
      v.push_back(make_pair(p, -0.5));
  } else if (dcmp(d2) == 0) {
    long double p = getp(st, la, lb);
    if (dcmp(ds * dl) > 0)
      v.push_back(make_pair(p, 0.5));
    else
      v.push_back(make_pair(p, -0.5));
  } else {
    vec c = ss + ds * (d1 / (d1 + d2));
    long double p = getp(c, la, lb);
    if (dcmp(ds * dl) > 0)
      v.push_back(make_pair(p, 1));
    else
      v.push_back(make_pair(p, -1));
  }
}
int n, m;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%Lf%Lf", &p[i].x, &p[i].y);
  p[n] = p[0];
  for (int i = 1; i <= m; i++) {
    vec A, B;
    scanf("%Lf%Lf%Lf%Lf", &A.x, &A.y, &B.x, &B.y);
    long double ans = 0;
    v.clear();
    for (int j = 0; j < n; j++) intersect(p[j], p[j + 1], A, B);
    sort(v.begin(), v.end());
    double r = 0;
    for (int j = 0; j < v.size(); j++) {
      r += v[j].second;
      if (dcmp(r)) ans += v[j + 1].first - v[j].first;
    }
    ans *= (B - A).len();
    printf("%.13Lf\n", ans);
  }
  return 0;
}

```