Jealous Teachers

Time limit3sMemory limit1024 MB

Summary
Each of N-1 students must send exactly N-1 flowers to teachers they learned from, and every teacher must receive exactly N-1 flowers in total, or report that no such assignment exists.
Level

Medium6 of 10

Topics
Graph, Implementation, Greedy, Array
Solved
No attempts yet

Problem

The Korea Science Academy of KAIST (KSA) has NN teachers and NN students. Because tomorrow is Teacher's Day in Korea, each student bought NN flowers. However, one student quit, so only N−1N-1 students remain at the school.

The teachers are very jealous, so they give an F grade to a student who gives them fewer flowers than the other students do. Therefore, every teacher must receive exactly N−1N-1 flowers. A student can give flowers only to teachers who have taught that student, and you are given which students learned from which teachers.

Seunghyun is a student at KSA, and he needs your help organizing this event.

Input

The first line contains two integers NN and MM, the number of teachers and the number of (student, teacher) pairs where the student learned from the teacher.

The next MM lines describe the relations. The jj-th line contains two integers sjs_j and tjt_j, meaning that the sjs_j-th student can give flowers to the tjt_j-th teacher. All pairs are distinct.

Output

If it is impossible to give all teachers the same number of flowers (N−1N-1 flowers), print −1-1 on the first line.

Otherwise, output MM lines. The jj-th line must contain a single integer, the number of flowers that the sjs_j-th student gave to the tjt_j-th teacher.

If there are multiple possible answers, you may output any of them.

Constraints

  • 2≤N≤100 0002 \le N \le 100\,000
  • 1≤M≤200 0001 \le M \le 200\,000
  • 1≤sj≤N−11 \le s_j \le N-1 (1≤j≤N1 \le j \le N)
  • 1≤tj≤N1 \le t_j \le N (1≤i≤N1 \le i \le N)

Examples2

  1. Example 1

    Input
    6 12
    1 3
    1 4
    1 5
    2 2
    2 4
    3 1
    3 3
    4 1
    4 2
    4 4
    5 4
    5 6
    Expected output
    1
    0
    5
    5
    1
    2
    4
    3
    0
    3
    1
    5
    
  2. Example 2

    Input
    6 12
    1 2
    1 3
    1 4
    2 2
    2 4
    3 1
    3 3
    4 1
    4 2
    4 4
    5 5
    5 6
    Expected output
    -1