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

Solution (in JAVA):

Solution:
import java.io.*;
import java.util.*;

public class cfEdu1E {

	BufferedReader br;
	PrintWriter out;
	StringTokenizer st;
	boolean eof;

	/**
	 * @return x * 10^afterDot, where x is a value of number represented by
	 *         <i>s</i>
	 * @throws IllegalArgumentException
	 *             - if there are more than <i>afterDot</i> digits after the dot
	 *             Overflows are not handled at all.
	 */
	static long parseDecimal(String s, int afterDot) {
		if (s.charAt(0) == '-') {
			return -parseDecimal(s.substring(1), afterDot);
		}
		int dot = s.indexOf('.');
		long p10 = 1;
		for (int i = 0; i < afterDot; i++) {
			p10 *= 10;
		}
		if (dot == -1) {
			return Long.parseLong(s) * p10;
		}
		long whole = Long.parseLong(s.substring(0, dot));
		long frac = Long.parseLong(s.substring(dot + 1));
		int len = s.length() - dot - 1;
		if (len > afterDot) {
			throw new IllegalArgumentException("Too many digis after the dot");
		}
		while (len < afterDot) {
			len++;
			frac *= 10;
		}
		return whole * p10 + frac;
	}

	static class IntPoint {
		int x, y;

		public IntPoint(int x, int y) {
			this.x = x;
			this.y = y;
		}
	}

	static int getTurn(IntPoint o, IntPoint p, IntPoint q) {
		int x1 = p.x - o.x;
		int y1 = p.y - o.y;
		int x2 = q.x - o.x;
		int y2 = q.y - o.y;
		return Long.signum((long) x1 * y2 - (long) x2 * y1);
	}

	static long vectProd(IntPoint a, IntPoint b) {
		return (long) a.x * b.y - (long) a.y * b.x;
	}

	static final double EPS = 1e-9;

	static int comp(double x, double y) {
		return Math.abs(x - y) < EPS ? 0 : (x < y ? -1 : 1);
	}

	static class Event implements Comparable<Event> {
		double x, y;
		int type; // 0 -> cross, -1/1 - from corresponding side to edge

		@Override
		public int compareTo(Event arg0) {
			int tmp = comp(x, arg0.x);
			return tmp != 0 ? tmp : comp(y, arg0.y);
		}

		private Event(int side1, int side2) {
			type = side1 + side2;
		}

		public Event(IntPoint a, IntPoint b, IntPoint c, IntPoint d, int side1,
				int side2) {
			this(side1, side2);
			long denom = (long) (b.x - a.x) * (d.y - c.y) - (long) (b.y - a.y)
					* (d.x - c.x);
			long axb = vectProd(a, b);
			long cxd = vectProd(c, d);
			// numXY can be ~10^21
			double numX = 1.0 * (b.x - a.x) * cxd - 1.0 * (d.x - c.x) * axb;
			double numY = 1.0 * (b.y - a.y) * cxd - 1.0 * (d.y - c.y) * axb;
			x = numX / denom;
			y = numY / denom;
		}

		public Event(IntPoint a, int side1, int side2) {
			this(side1, side2);
			x = a.x;
			y = a.y;
		}
	}

	double go(IntPoint[] a, int n, IntPoint p, IntPoint q) {
		int[] side = new int[n + 2];
		for (int i = 0; i < n + 2; i++) {
			side[i] = getTurn(p, q, a[i]);
		}
		List<Event> evs = new ArrayList<>();
		for (int i = 1; i <= n; i++) {
			if (side[i] * side[i + 1] == -1) {
				evs.add(new Event(a[i], a[i + 1], p, q, side[i], side[i + 1]));
			}
			if (side[i] == 0 && side[i - 1] != side[i + 1]) {
				evs.add(new Event(a[i], side[i - 1], side[i + 1]));
			}
		}

		double sx = 0, sy = 0;

		Collections.sort(evs);
		boolean curIn = false;
		int edgeState = 0; // 0 -> not on edge, -1/1 -> on edge from
							// corresponding side
		for (int i = 0; i < evs.size(); i++) {
			Event e = evs.get(i);
			if (edgeState != 0 || curIn) {
				sx += e.x - evs.get(i - 1).x;
				sy += e.y - evs.get(i - 1).y;
			}
			if (edgeState == 0) {
				if (e.type == 0) {
					curIn ^= true;
				} else {
					edgeState = e.type;
				}
			} else {
				if (e.type == 0) {
					throw new AssertionError();
				} else {
					curIn ^= (e.type != edgeState);
					edgeState = 0;
				}
			}
		}
		return Math.sqrt(sx * sx + sy * sy);
	}

	void solve() throws IOException {
		int n = nextInt();
		int m = nextInt();

		IntPoint[] a = new IntPoint[n + 2];
		for (int i = 1; i <= n; i++) {
			int x = (int) parseDecimal(nextToken(), 2);
			int y = (int) parseDecimal(nextToken(), 2);
			a[i] = new IntPoint(x, y);
		}
		a[0] = a[n];
		a[n + 1] = a[1];

		for (int i = 0; i < m; i++) {
			int x1 = (int) parseDecimal(nextToken(), 2);
			int y1 = (int) parseDecimal(nextToken(), 2);
			int x2 = (int) parseDecimal(nextToken(), 2);
			int y2 = (int) parseDecimal(nextToken(), 2);
			out.println(go(a, n, new IntPoint(x1, y1), new IntPoint(x2, y2)) / 100);
		}
	}

	cfEdu1E() throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		solve();
		out.close();
	}

	public static void main(String[] args) throws IOException {
		new cfEdu1E();
	}

	String nextToken() {
		while (st == null || !st.hasMoreTokens()) {
			try {
				st = new StringTokenizer(br.readLine());
			} catch (Exception e) {
				eof = true;
				return null;
			}
		}
		return st.nextToken();
	}

	String nextString() {
		try {
			return br.readLine();
		} catch (IOException e) {
			eof = true;
			return null;
		}
	}

	int nextInt() throws IOException {
		return Integer.parseInt(nextToken());
	}

	long nextLong() throws IOException {
		return Long.parseLong(nextToken());
	}

	double nextDouble() throws IOException {
		return Double.parseDouble(nextToken());
	}
}