This page is still under construction.

Parts of this page are still being built. What you see may change.

Milking Order

Time limit2sMemory limit512 MB

Summary
Find the largest prefix of M ordered cow lists that stays acyclic, then output the lexicographically smallest topological order of that graph.
Level

Hard8 of 10

Topics
Graph, Topological sort, Binary search, Greedy
Solved
No attempts yet

Problem

Farmer John's NN cows (1≤N≤1051 \leq N \leq 10^5), numbered 1…N1 \ldots N as always, have far too much time on their hooves. They have worked out a complex social hierarchy around the order in which Farmer John milks them every morning.

After weeks of study, Farmer John has made MM observations about that hierarchy (1≤M≤50 0001 \leq M \leq 50\,000). Each observation is an ordered list of some of his cows, and it means those cows must be milked in the order in which they appear in the list. For example, if one observation is the list 2, 5, 1, Farmer John must milk cow 2 sometime before cow 5, and cow 5 sometime before cow 1.

The observations are prioritized, so Farmer John wants to maximize the value of XX for which his milking order meets the conditions of the first XX observations. If several milking orders satisfy those first XX conditions, a longstanding tradition says that cows with lower numbers outrank cows with higher numbers, so Farmer John milks the lowest-numbered cows first. More formally, he uses the lexicographically smallest such ordering. An ordering xx is lexicographically smaller than an ordering yy if for some jj, xi=yix_i = y_i for all i<ji < j and xj<yjx_j < y_j: the two orderings agree up to a point, at which xx is smaller.

Help Farmer John determine the best order in which to milk his cows.

Input

The first line contains NN and MM. Each of the next MM lines describes one observation. Line i+1i+1 describes observation ii: it starts with the number of cows mim_i listed in the observation, followed by the mim_i cow numbers in the observed order. The sum of the mim_i is at most 200 000200\,000.

Output

Output NN space-separated integers forming a permutation of 1…N1 \ldots N, the order in which Farmer John should milk his cows.

Note

In the first example, Farmer John has four cows. The first observation says to milk cow 1 before cow 2 and cow 2 before cow 3. The second says to milk cow 4 before cow 2, and the third says to milk cow 3 before cow 4 and cow 4 before cow 1. The first two observations hold together, but all three at once would require cow 1 before cow 3 and cow 3 before cow 1, which is impossible. That leaves two possible orderings, 1 4 2 3 and 4 1 2 3, and the lexicographically smaller one is 1 4 2 3.

Examples2

  1. Example 1

    Input
    4 3
    3 1 2 3
    2 4 2
    3 3 4 1
    
    Expected output
    1 4 2 3
    
  2. Example 2

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