#C107. The Town Judge Problem

    ID: 39933 Type: Default 1000ms 256MiB

The Town Judge Problem

The Town Judge Problem

In a town of n people labeled from 1 to n, there exists a rumor about a town judge. The town judge is defined by the following properties:

  • The judge trusts no one.
  • Every other person (except the judge) trusts the judge.

Formally, if the trust relationships are given as a list of ordered pairs \((a, b)\) (meaning person \(a\) trusts person \(b\)), then the town judge is the person \(j\) such that:

\[ \begin{cases} \text{trustCount}(j)=0\\ \text{trustedBy}(j)=n-1 \end{cases} \]

Note that if there is only one person in town and no trust relationships, then that person is the judge. If no such person exists, output \(-1\).

inputFormat

The input is given from stdin in the following format:

n
m
a1 b1
... 
am bm

Where:

  • n is the number of people in the town.
  • m is the number of trust relationships.
  • Each of the next m lines contains two integers a and b (separated by space), indicating that person a trusts person b.

outputFormat

Output to stdout a single integer representing the label of the town judge if one exists, or \(-1\) if no judge exists.

## sample
1
0
1

</p>