import java.io.*;
import java.util.*;

// Задача "Голова на плечах"
// 10-ая Интернет-олимпиада сезона 2006-2007, базовый уровень
// Автор задачи: Максим Буздалов, buzdalov@rain.ifmo.ru
// Автор решения: Максим Буздалов, buzdalov@rain.ifmo.ru

// Решение, проверяющее корректность входных данных.

public class headand_mb_assert {
    static void myAssert(boolean exp, String message) {
        if (!exp) throw new AssertionError(message);
    }

    static class Hair {
        double start, finish;

        Hair(String line, int R, int K) {
            StringTokenizer st = new StringTokenizer(line);
            double xs = Double.parseDouble(st.nextToken());
            double ys = Double.parseDouble(st.nextToken());
            
            double xe = finish = Double.parseDouble(st.nextToken());
            double ye = K;

            myAssert(Math.abs(Math.hypot(xs, ys) - R) < 1e-6, "Head point does not lie on head");
            myAssert(Math.abs(Double.parseDouble(st.nextToken()) - K) < 1e-6, "Wrong Y_s coordinate");

            double a = (xe - xs) * (xe - xs) + (ye - ys) * (ye - ys);
            double b = xs * (xe - xs) + ys * (ye - ys);
            double c = xs * xs + ys * ys - R * R;
            double d = b * b - a * c;
            if (d < 0) d = 0;
            double t1 = (-b + Math.sqrt(d)) / a;
            double t2 = (-b - Math.sqrt(d)) / a;

            //Positive t's are impossible <=== R < -K
            myAssert(
                (t1 < 1e-6) && (t2 < 1e-6), 
                "One more cross of hair and head detected: t1 = " + t1 + ", t2 = " + t2 + ", xh = " + xs + ", yh = " + ys + ", xs = " + xe + ", ys = " + ye + ", D = " + d
            );

            start = Math.atan2(ys, xs);
            if (start > Math.PI / 2) {
                start -= Math.PI * 2;
            }
        }
    }

    static class StartComparator implements Comparator<Hair> {
        public int compare(Hair a, Hair b) {
            if (a.start < b.start) return -1;
            if (a.start > b.start) return 1;
            return 0; //не бывает
        }
    }

    static class FinishComparator implements Comparator<Hair> {
        public int compare(Hair a, Hair b) {
            if (a.finish < b.finish) return -1;
            if (a.finish > b.finish) return 1;
            return 0; //не бывает
        }
    }

    static long count;

    static Hair[] hair, temp;

    static Comparator<Hair> c = new FinishComparator();

    static void mergeSort(int s, int e) {  
        if (s >= e) return;
        int m = (s + e) / 2;

        mergeSort(s, m);
        mergeSort(m + 1, e);

        int i = s, j = m + 1, k = s;
        while (k <= e) {
            if (i > m) {
                count += Math.abs(j - k);
                temp[k++] = hair[j++];
            } else if (j > e) {
                count += Math.abs(i - k);
                temp[k++] = hair[i++];
            } else if (c.compare(hair[i], hair[j]) < 0) {
                count += Math.abs(i - k);
                temp[k++] = hair[i++];
            } else {
                count += Math.abs(j - k);
                temp[k++] = hair[j++];
            }
        }

        for (i = s; i <= e; i++) {
            hair[i] = temp[i];
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new FileReader("headand.in"));
        PrintWriter out = new PrintWriter("headand.out");

        StringTokenizer st = new StringTokenizer(in.readLine());
        int R = Integer.parseInt(st.nextToken());
        int K = Integer.parseInt(st.nextToken());
        int N = Integer.parseInt(in.readLine());

        myAssert(1 <= R && R <= K, "Radius of head is out of bounds");
        myAssert(K <= 1000, "Shoulder level is out of bounds");
        myAssert(0 <= N && N <= 100000, "The hair cardinality is out of bounds");

        hair = new Hair[N];
        temp = new Hair[N];
        for (int i = 0; i < N; i++) {
            hair[i] = new Hair(in.readLine(), R, -K);
        }

        long t0 = System.currentTimeMillis();
        Arrays.sort(hair, new StartComparator());
        long t1 = System.currentTimeMillis();

        for (int i = 1; i < N; i++) {
            myAssert(Math.abs(hair[i - 1].start - hair[i].start) > 1e-9, "Two starts coincide");
        }

        count = 0;
        long t2 = System.currentTimeMillis();
        mergeSort(0, N - 1);
        long t3 = System.currentTimeMillis();

        for (int i = 1; i < N; i++) {
            myAssert(Math.abs(hair[i - 1].finish - hair[i].finish) > 1e-9, "Two finishes coincide");
        }

        System.err.println((t3 - t2) + " " + (t1 - t0));

        out.println(count / 2);
        out.close();
        in.close();
    }
}