#include <bits/stdc++.h>
const long double INF = 1e100;
const long double eps = 1e-9;
const long double PI = acos(-1);
struct P {
  long double x, y;
  P() { x = 0, y = 0; }
  P(long double x, long double y) : x(x), y(y) {}
  P add(P p) { return {x + p.x, y + p.y}; }
  P sub(P p) { return {x - p.x, y - p.y}; }
  long double dot(P p) { return x * p.x + y * p.y; }
  long double det(P p) { return x * p.y - p.x * y; }
  P mul(long double p) { return {x * p, y * p}; }
  P div(long double p) { return {x / p, y / p}; }
  long double abs() { return sqrt(((x) * (x)) + ((y) * (y))); }
  long double abs2() { return ((x) * (x)) + ((y) * (y)); }
  P rot90() { return {-y, x}; }
  bool operator<(const P &p) const & {
    if (std::abs(p.x - x) > eps) {
      return x < p.x;
    }
    return y < p.y;
  }
  bool operator==(const P &p) const & {
    return std::abs(p.x - x) < eps && std::abs(p.y - y) < eps;
  }
};
int sig(long double p) {
  if (std::abs(p) < eps) {
    return 0;
  }
  return p > 0 ? 1 : -1;
}
bool crsLS(P p1, P p2, P q1, P q2) {
  return sig(p1.sub(q1).det(p2.sub(q1))) * sig(p1.sub(q2).det(p2.sub(q2))) <= 0;
}
bool onLine(P p1, P p2, P q) {
  return std::abs(p2.sub(p1).det(q.sub(p1))) < eps;
}
P isLL(P p1, P p2, P q1, P q2) {
  long double d = q2.sub(q1).det(p2.sub(p1));
  if (std::abs(d) < eps) {
    return {INF, INF};
  }
  return p1.add(p2.sub(p1).mul(q2.sub(q1).det(q1.sub(p1)) / d));
}
bool parallel(P p1, P p2, P q1, P q2) {
  return std::abs(p2.sub(p1).det(q2.sub(q1))) < eps;
}
std::vector<P> vec, aux;
std::vector<std::pair<P, std::pair<int, int>>> _vec;
int n, test;
int main() {
  scanf("%d%d", &n, &test);
  for (int i = 0; i < n; ++i) {
    double x, y;
    scanf("%lf%lf", &x, &y);
    vec.push_back({x, y});
  }
  for (int i = 0; i < n; ++i) {
    P p1 = vec[(i - 1 + n) % n], p2 = vec[i], p3 = vec[(i + 1) % n];
    if (!parallel(p1, p2, p2, p3)) {
      aux.push_back(p2);
    }
  }
  vec = aux;
  n = vec.size();
  while (test--) {
    _vec.clear();
    double x1, y1, x2, y2;
    P p1, p2;
    scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
    p1 = {x1, y1};
    p2 = {x2, y2};
    long double ans = 0;
    for (int i = 0; i < n; ++i) {
      P q1 = vec[i], q2 = vec[(i + 1) % n];
      if (!parallel(p1, p2, q1, q2)) {
        if (crsLS(p1, p2, q1, q2)) {
          P p = isLL(p1, p2, q1, q2);
          if (!(p == q1) && !(p == q2)) {
            _vec.push_back({p, {1, 1}});
          }
        }
      } else {
        if (onLine(p1, p2, q1)) {
          P q3 = vec[(i - 1 + n) % n], q4 = vec[(i + 2) % n];
          int sig1 = sig(q3.sub(q1).det(p2.sub(p1))),
              sig2 = sig(q4.sub(q2).det(p2.sub(p1)));
          if (q2 < q1) {
            std::swap(q1, q2);
          }
          if (sig1 * sig2 == 1) {
            _vec.push_back({q1, {0, 0}});
            _vec.push_back({q2, {0, 0}});
          } else {
            _vec.push_back({q1, {0, 0}});
            _vec.push_back({q2, {0, 1}});
          }
          ans += q1.sub(q2).abs();
        }
      }
    }
    for (int i = 0; i < n; ++i) {
      if (!onLine(p1, p2, vec[i])) {
        continue;
      }
      P q1 = vec[(i - 1 + n) % n], q2 = vec[(i + 1) % n];
      int sig1 = sig(q1.sub(vec[i]).det(p2.sub(p1))),
          sig2 = sig(q2.sub(vec[i]).det(p2.sub(p1)));
      if (sig1 && sig2 && sig1 * sig2 != 1) {
        _vec.push_back({vec[i], {1, 1}});
      }
    }
    std::sort(_vec.begin(), _vec.end());
    if (!vec.size()) {
      printf("%.10lf\n", 0.0);
      continue;
    }
    int now = 0;
    bool flag = false;
    for (int i = 0, sz = _vec.size() - 1; i < sz; ++i) {
      now ^= _vec[i].second.second;
      if (flag) {
        flag = false;
        if (now) {
          ans += (_vec[i + 1].first).sub(_vec[i].first).abs();
        }
        continue;
      }
      if (!_vec[i].second.first) {
        flag = true;
        continue;
      }
      if (now) {
        ans += (_vec[i + 1].first).sub(_vec[i].first).abs();
      }
    }
    printf("%.10lf\n", (double)ans);
  }
  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>
const long double INF = 1e100;
const long double eps = 1e-9;
const long double PI = acos(-1);
struct P {
  long double x, y;
  P() { x = 0, y = 0; }
  P(long double x, long double y) : x(x), y(y) {}
  P add(P p) { return {x + p.x, y + p.y}; }
  P sub(P p) { return {x - p.x, y - p.y}; }
  long double dot(P p) { return x * p.x + y * p.y; }
  long double det(P p) { return x * p.y - p.x * y; }
  P mul(long double p) { return {x * p, y * p}; }
  P div(long double p) { return {x / p, y / p}; }
  long double abs() { return sqrt(((x) * (x)) + ((y) * (y))); }
  long double abs2() { return ((x) * (x)) + ((y) * (y)); }
  P rot90() { return {-y, x}; }
  bool operator<(const P &p) const & {
    if (std::abs(p.x - x) > eps) {
      return x < p.x;
    }
    return y < p.y;
  }
  bool operator==(const P &p) const & {
    return std::abs(p.x - x) < eps && std::abs(p.y - y) < eps;
  }
};
int sig(long double p) {
  if (std::abs(p) < eps) {
    return 0;
  }
  return p > 0 ? 1 : -1;
}
bool crsLS(P p1, P p2, P q1, P q2) {
  return sig(p1.sub(q1).det(p2.sub(q1))) * sig(p1.sub(q2).det(p2.sub(q2))) <= 0;
}
bool onLine(P p1, P p2, P q) {
  return std::abs(p2.sub(p1).det(q.sub(p1))) < eps;
}
P isLL(P p1, P p2, P q1, P q2) {
  long double d = q2.sub(q1).det(p2.sub(p1));
  if (std::abs(d) < eps) {
    return {INF, INF};
  }
  return p1.add(p2.sub(p1).mul(q2.sub(q1).det(q1.sub(p1)) / d));
}
bool parallel(P p1, P p2, P q1, P q2) {
  return std::abs(p2.sub(p1).det(q2.sub(q1))) < eps;
}
std::vector<P> vec, aux;
std::vector<std::pair<P, std::pair<int, int>>> _vec;
int n, test;
int main() {
  scanf("%d%d", &n, &test);
  for (int i = 0; i < n; ++i) {
    double x, y;
    scanf("%lf%lf", &x, &y);
    vec.push_back({x, y});
  }
  for (int i = 0; i < n; ++i) {
    P p1 = vec[(i - 1 + n) % n], p2 = vec[i], p3 = vec[(i + 1) % n];
    if (!parallel(p1, p2, p2, p3)) {
      aux.push_back(p2);
    }
  }
  vec = aux;
  n = vec.size();
  while (test--) {
    _vec.clear();
    double x1, y1, x2, y2;
    P p1, p2;
    scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
    p1 = {x1, y1};
    p2 = {x2, y2};
    long double ans = 0;
    for (int i = 0; i < n; ++i) {
      P q1 = vec[i], q2 = vec[(i + 1) % n];
      if (!parallel(p1, p2, q1, q2)) {
        if (crsLS(p1, p2, q1, q2)) {
          P p = isLL(p1, p2, q1, q2);
          if (!(p == q1) && !(p == q2)) {
            _vec.push_back({p, {1, 1}});
          }
        }
      } else {
        if (onLine(p1, p2, q1)) {
          P q3 = vec[(i - 1 + n) % n], q4 = vec[(i + 2) % n];
          int sig1 = sig(q3.sub(q1).det(p2.sub(p1))),
              sig2 = sig(q4.sub(q2).det(p2.sub(p1)));
          if (q2 < q1) {
            std::swap(q1, q2);
          }
          if (sig1 * sig2 == 1) {
            _vec.push_back({q1, {0, 0}});
            _vec.push_back({q2, {0, 0}});
          } else {
            _vec.push_back({q1, {0, 0}});
            _vec.push_back({q2, {0, 1}});
          }
          ans += q1.sub(q2).abs();
        }
      }
    }
    for (int i = 0; i < n; ++i) {
      if (!onLine(p1, p2, vec[i])) {
        continue;
      }
      P q1 = vec[(i - 1 + n) % n], q2 = vec[(i + 1) % n];
      int sig1 = sig(q1.sub(vec[i]).det(p2.sub(p1))),
          sig2 = sig(q2.sub(vec[i]).det(p2.sub(p1)));
      if (sig1 && sig2 && sig1 * sig2 != 1) {
        _vec.push_back({vec[i], {1, 1}});
      }
    }
    std::sort(_vec.begin(), _vec.end());
    if (!vec.size()) {
      printf("%.10lf\n", 0.0);
      continue;
    }
    int now = 0;
    bool flag = false;
    for (int i = 0, sz = _vec.size() - 1; i < sz; ++i) {
      now ^= _vec[i].second.second;
      if (flag) {
        flag = false;
        if (now) {
          ans += (_vec[i + 1].first).sub(_vec[i].first).abs();
        }
        continue;
      }
      if (!_vec[i].second.first) {
        flag = true;
        continue;
      }
      if (now) {
        ans += (_vec[i + 1].first).sub(_vec[i].first).abs();
      }
    }
    printf("%.10lf\n", (double)ans);
  }
  return 0;
}

```