// Arup Guha
// 11/3/2013
// Solution to 2013 South East Regional Division 2 Problem A: Cut the Cake

import java.util.*;

public class cake {

	public static int radius;
	public static pt center;
	public static line[] lines;
	public static int n;

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);

		// Get first case.
		radius = stdin.nextInt();
		double x = stdin.nextDouble();
		double y = stdin.nextDouble();
		center = new pt(x,y);
		n = stdin.nextInt();

		// Process each case.
		while (radius != 0) {

			// Read in all the lines.
			lines = new line[n];
			for (int i=0; i<n; i++) {
				int x1 = stdin.nextInt();
				int y1 = stdin.nextInt();
				int x2 = stdin.nextInt();
				int y2 = stdin.nextInt();
				lines[i] = new line(x1, y1, x2, y2);
			}

			System.out.println(solve());

			// Get next case.
			radius = stdin.nextInt();
			x = stdin.nextDouble();
			y = stdin.nextDouble();
			center = new pt(x,y);
			n = stdin.nextInt();
		}
	}

	public static int solve() {

		/*** We can ignore all circle-line intersections because the number of vertices always equals the number of edges
		 *   and couonting these intersections won't affect the final result from Euler's Planar Graph formula.
		 ***/

		int numV = 0, numE = 0;

		// Look for each intersection with line i.
		for (int i=0; i<n; i++) {

			if (lines[i].dist(center) >= radius) continue;

			// Count them up here.
			int numIntersections = 0;
			for (int j=0; j<n; j++) {

				if (j == i) continue;

				// Try this intersection.
				pt cross = lines[i].intersect(lines[j]);
				if (cross == null) continue;

				// We only count it if it's in the circle.
				if (center.dist(cross) < radius)
					numIntersections++;
			}

			// Update vertex count and edge count.
			numV += numIntersections;
			numE += (numIntersections+1);
		}

		// Update since we counted each vertex twice.
		numV /= 2;

		// Use Euler's formula and subtract one for the area outside the circle.
		return 1 - numV + numE;
	}
}

class pt {

	public double x;
	public double y;

	public pt(double myx, double myy) {
		x = myx;
		y = myy;
	}

	public double mag() {
		return Math.sqrt(x*x+y*y);
	}

	public static double crossMag(pt a, pt b) {
		return Math.abs(a.x*b.y-b.x*a.y);
	}

	public double dist(pt other) {
		return Math.sqrt(Math.pow(this.x-other.x,2) + Math.pow(this.y-other.y,2));
	}
}

class line {

	public pt start;
	public pt end;
	public pt vect;

	public line(double x1, double y1, double x2, double y2) {
		start = new pt(x1, y1);
		end = new pt(x2, y2);
		vect = new pt(x2-x1, y2-y1);
	}

	// Returns the distance from this line to p.
	public double dist(pt p) {
		pt vToP = new pt(p.x-start.x, p.y-start.y);
		return Math.abs(pt.crossMag(vToP, vect)/vect.mag());
	}

	public pt intersect(line other) {

		// Set up two equations a(lambda) + b(beta) = c and d(lambda) + e(beta) = f, vector equations
		// equating the point of intersection of the two lines.
		double a = this.vect.x;
		double b = - other.vect.x;
		double c = other.start.x - this.start.x;
		double d = this.vect.y;
		double e = -other.vect.y;
		double f = other.start.y - this.start.y;

		// Use Kramers.
		double den = det(a,b,d,e);
		if (den == 0) return null;

		// Use lambda on this line to find the intersection point.
		double lambda = det(c,b,f,e)/den;
		return new pt(this.start.x+lambda*this.vect.x, this.start.y+lambda*this.vect.y);
	}

	// Returns the determinant with row1 (a,b), row2 (c,d).
	public static double det(double a, double b, double c, double d) {
		return a*d - b*c;
	}
}