This page is still under construction.

Parts of this page are still being built. What you see may change.

Loopy transit

Time limit2sMemory limit256 MB

Summary
Count distinct directed simple cycles in a graph of up to 9 stations, treating rotations of a loop as one.
Level

Medium6 of 10

Topics
Graph, Backtracking, Brute force
Solved
No attempts yet

Problem

Luke likes riding public transit in the cities he visits, just for fun. What he enjoys most is finding loops: he leaves one station, travels along the connections through at least one other station, and comes back to the station he started from. He wants to know how many such loops each transit system has.

What Luke counts is simple loops. A simple loop is a sequence of distinct stations t1,t2,…,tjt_1, t_2, \dots, t_j such that there is a direct connection from tit_i to ti+1t_{i+1} for every ii with 1≤i<j1 \le i < j, and a direct connection from tjt_j to t1t_1 as well. A loop can be written down starting from any of its stations, so every cyclic shift of such a sequence is the same simple loop. Two simple loops that visit the same set of stations in a different order are counted as different loops.

Write a program that counts the distinct simple loops in the transit system.

Input

The first line contains the number of stations mm in the transit system (3≤m≤93 \le m \le 9). Stations are numbered 00 to m−1m-1.

The second line contains the number of connections nn (1≤n≤m(m−1)1 \le n \le m(m-1)). Each of the next nn lines describes one connection as two integers ss and tt (0≤s<m0 \le s < m, 0≤t<m0 \le t < m, s≠ts \ne t), meaning there is a one-way connection from station ss to station tt.

Output

Print the number of distinct simple loops in the transit system.

Examples3

  1. Example 1

    Input
    5
    5
    0 1
    1 2
    2 3
    3 4
    4 2
    
    Expected output
    1
    
  2. Example 2

    Input
    8
    10
    0 1
    1 2
    2 3
    3 4
    4 5
    5 0
    2 6
    6 0
    3 7
    7 0
    
    Expected output
    3
    
  3. Example 3

    Input
    4
    8
    0 1
    1 2
    2 3
    3 0
    1 0
    2 1
    3 2
    0 3
    
    Expected output
    6