아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Inspection

시간 제한1초메모리 제한1024 MB

요약
마을 사이의 일방통행 도로 그래프가 주어질 때, 왕복 여행이 가능하도록 새로 지어야 하는 최소 도로 수를 구하거나 불가능하면 -1을 출력한다.
난이도

보통10점 중 6점

유형
그래프, 유니온 파인드
정답자
아직 제출이 없습니다

문제

A government inspector is coming to your county to check the quality of roads. You want to pass the inspection with minimal effort and expenses, so you're planning to take the inspector on a planned round trip between towns, in order to avoid repairing roads all over the county. Any road in the county goes straight from one town to another, and all roads are one-way. To drive the inspector around successfully, you may have to build new roads. Note that roads going out of a town and back again to the same town are forbidden, and building them even to fool state inspectors is considered bad taste. However, you can build as many roads between any two towns as you want, with arbitrary directions. Define the minimal number of roads to be built in order to take the inspector on a round trip. The length of the trip does not matter.

입력

The first line of the input file contains two integers: NN --- the number of towns and MM --- the number of one-way roads in the county (1≤N≤1051 \le N \le 10^5, 0≤M≤1050 \le M \le 10^5).

The remaining MM lines of the file contain the descriptions of the available roads in the county. For each road, a separate line contains two integers: AA --- the number of the town where the road begins and BB --- the number of the town where the road ends (1≤A≠B≤N1 \le A \neq B \le N). All towns are numbered with integers from 11 to NN.

출력

The output file must contain a single integer --- the minimal number of roads that must be built in order to be able to take the inspector on a round trip between the towns. If that is impossible, print −1-1.

예제2

  1. 예제 1

    입력
    2 5
    2 1
    2 1
    2 1
    2 1
    1 2
    
    예상 출력
    0
    
  2. 예제 2

    입력
    3 2
    2 3
    1 2
    
    예상 출력
    1