Vectors#676

Vectors

1 second
64 megabytes
Easy

On the plane, there is a set of points , where and are integers that satisfy 1 \leq x \leq M and 1 \leq y \leq N. From each point, exactly one of the following vectors emerges: , , , , , , , . Each vector connects one integer point to another. For example, if the point has the vector , then it is connected to the point , but not vice versa.

A sequence of two or more points a_1, a_2, \dots, a_k on the plane is called a cycle if for each 1 \leq i \leq k-1, point a_i is connected by its vector to a_{i+1}, and a_k is connected by its vector to a_1. Cycles are considered different if they differ by at least one vertex.

Write a program that, given information about the vectors from all the points, finds the number of distinct cycles formed.

prb676

Input

The first line contains two integers (1 \leq N \leq 100) and (1 \leq M \leq 100). Each of the following lines contains numbers: pairs of integers. Each pair in row and column specifies the vector for the point .

Output

Print a single integer — the number of cycles formed by the vectors.

Examples

Example 1

Loading
Loading