Course Prerequisites Order

MEDIUM
G
Time limit: 2.0sMemory limit: 256MB
graphstopological-sortgreedy

Course Prerequisites Order

There are n courses numbered 1..n and m prerequisite pairs (a, b) meaning course a must be completed ​‌‌‌​‌​‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​‌‌​​​​‌​​‌‌‌​‌​​‌‌​​​‌‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​​‌‌‌​‌​​‌‌‌​​​​​​‌‌‌‌​‌​​‌‌‌​​‌​‌‌​​‌‌​​​‌‌​​​​​​‌‌​‌​‌​​‌‌‌​​​​​‌‌​​​​​‌‌​​‌‌​​​‌‌​​‌‌​​‌‌‌​‌​​‌‌‌​‌​​​​‌‌‌‌​‌​​‌‌​​​‌​​‌‌​‌‌‌​​‌‌‌​​‌​​‌‌​​‌​​​‌‌‌​​​​​‌‌​​‌‌​​‌‌​‌‌‌​​‌‌‌​​​​​‌‌​​​​​​‌‌​​​​​​‌‌​‌​‌​​‌‌​‌‌‌​​‌‌‌​​‌before course b. Determine a valid order to take all n courses.

If multiple valid orders exist, output the lexicographically smallest one (compare the sequences of course numbers as you would compare arrays: find the first position where they differ). If it is impossible to take all courses (a ​‌‌‌​‌​‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​‌‌​​​​‌​​‌‌‌​‌​​‌‌​​​‌‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​​‌‌‌​‌​​‌‌‌​​​​​​‌‌‌‌​‌​​‌‌‌​​‌​‌‌​​‌‌​​​‌‌​​​​​​‌‌​‌​‌​​‌‌‌​​​​​‌‌​​​​​‌‌​​‌‌​​​‌‌​​‌‌​​‌‌‌​‌​​‌‌‌​‌​​​​‌‌‌‌​‌​​‌‌​​​‌​​‌‌​‌‌‌​​‌‌‌​​‌​​‌‌​​‌​​​‌‌‌​​​​​‌‌​​‌‌​​‌‌​‌‌‌​​‌‌‌​​​​​‌‌​​​​​​‌‌​​​​​​‌‌​‌​‌​​‌‌​‌‌‌​​‌‌‌​​‌cycle exists), output IMPOSSIBLE instead.

Input

Line 1: two integers n m (1 ≤ n ≤ 2·10^5, 0 ≤ m ≤ 2·10^5) Next m lines: two integers a b — course a before course b.

Output

Either n space-separated integers (the lexicographically smallest valid order), or the single word IMPOSSIBLE. ​‌‌‌​‌​‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​‌‌​​​​‌​​‌‌‌​‌​​‌‌​​​‌‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​​‌‌‌​‌​​‌‌‌​​​​​​‌‌‌‌​‌​​‌‌‌​​‌​‌‌​​‌‌​​​‌‌​​​​​​‌‌​‌​‌​​‌‌‌​​​​​‌‌​​​​​‌‌​​‌‌​​​‌‌​​‌‌​​‌‌‌​‌​​‌‌‌​‌​​​​‌‌‌‌​‌​​‌‌​​​‌​​‌‌​‌‌‌​​‌‌‌​​‌​​‌‌​​‌​​​‌‌‌​​​​​‌‌​​‌‌​​‌‌​‌‌‌​​‌‌‌​​​​​‌‌​​​​​​‌‌​​​​​​‌‌​‌​‌​​‌‌​‌‌‌​​‌‌‌​​‌

Example

Input:

4 3
1 2
1 3
3 4

Output:

1 2 3 4

Constraints

  • 1 ≤ n ≤ 2·10^5, 0 ≤ m ≤ 2·10^5
  • 1 ≤ a, b ≤ n ​‌‌‌​‌​‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​‌‌​​​​‌​​‌‌‌​‌​​‌‌​​​‌‌​​‌‌‌‌​‌​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌‌​​‌‌‌​‌​​​‌‌​​‌​‌​‌‌‌​​‌‌​‌‌‌​‌​​​​‌‌‌​‌​​‌‌‌​​​​​​‌‌‌‌​‌​​‌‌‌​​‌​‌‌​​‌‌​​​‌‌​​​​​​‌‌​‌​‌​​‌‌‌​​​​​‌‌​​​​​‌‌​​‌‌​​​‌‌​​‌‌​​‌‌‌​‌​​‌‌‌​‌​​​​‌‌‌‌​‌​​‌‌​​​‌​​‌‌​‌‌‌​​‌‌‌​​‌​​‌‌​​‌​​​‌‌‌​​​​​‌‌​​‌‌​​‌‌​‌‌‌​​‌‌‌​​​​​‌‌​​​​​​‌‌​​​​​​‌‌​‌​‌​​‌‌​‌‌‌​​‌‌‌​​‌

Sample Testcases

Case 1

Input
3 0
Expected Output
1 2 3

Case 2

Input
4 3
1 2
1 3
3 4
Expected Output
1 2 3 4
Loading Cloud IDE...
Ln 1, Col 1Spaces: 4UTF-8
pythonAuto-Saved

Input

3 0

Expected Output

1 2 3

Run or submit your code to see output here.