#C5060. Longest Itinerary

    ID: 48668 Type: Default 1000ms 256MiB

Longest Itinerary

Longest Itinerary

You are given a directed graph representing a city’s intersections and one‐way roads. The intersections are numbered from 1 to (n) and there are (m) roads. Each road is represented by a pair of integers (u) and (v) indicating a direct road from intersection (u) to intersection (v).

Your task is to compute the length of the longest itinerary (i.e. the maximum number of intersections visited on a path) possible under the constraint that no cycle is allowed. If the graph contains a cycle (the itinerary would be undefined) then output (-1).

If there are no roads, the longest itinerary consists of a single intersection. Use standard input/output for processing.

inputFormat

The input is read from standard input (stdin) and has the following format:

  • The first line contains two integers (n) and (m), where (n) is the number of intersections and (m) is the number of roads.
  • The next (m) lines each contain two integers (u) and (v), representing a road from intersection (u) to intersection (v).

outputFormat

Print a single integer to standard output (stdout) representing the length of the longest itinerary. If the city graph contains a cycle, output (-1) instead.## sample

2 1
1 2
2

</p>