Prerequisite Courses

Given prerequisite pairs between courses, find the earliest semester each course can be completed when unlimited courses may be taken per semester.

Medium4GraphTopological sortDynamic programmingDFSInterviewNo attempts yetTime limit5sMemory limit256 MB

Problem

Minwook entered the computer science department of Z University this year, and he wants to take every major course the department offers before he graduates. Some courses have prerequisites: he can take such a course only after he has completed all of its prerequisites. Minwook refuses to give up on engineering accreditation, so he must respect every prerequisite. He wants to know when he can take each major course while respecting them. To keep the calculation simple he settled on these rules.

  1. There is no limit on how many courses he takes in one semester.
  2. Every course is offered every semester.

Write a program that computes, for every course, the smallest number of semesters needed to complete it.

Input

The first line contains the number of courses NN (1N10001 \le N \le 1000) and the number of prerequisite conditions MM (0M5000000 \le M \le 500\,000).

Each of the next MM lines contains one condition as two integers AA BB, meaning that course AA is a prerequisite of course BB. The input contains only conditions with A<BA < B (1A<BN1 \le A < B \le N). The same condition may appear more than once.

Output

Print, for courses 11 through NN in order, the earliest semester in which each course can be completed. Separate the numbers with single spaces on one line. The first semester counts as semester 1.

Hint

Suppose there are 3 courses, course 1 is a prerequisite of course 2, and course 2 is a prerequisite of course 3. Then Minwook completes course 1 in semester 1, course 2 in semester 2, and course 3 in semester 3.