Under construction

Still being built. Things may change

Diverse Singing

Time limit2sMemory limit512 MB

Summary
Pick a subset of acts so every singer sings at least once and every song is performed, with each language used at most once per singer and per song.
Level

Medium7 of 10

Topics
Graph, BFS
Solved
No attempts yet

Statement

In the modern world, diversity is a major concern, language diversity in particular. That is why the upcoming talent show will be held in multiple languages. However, making the program both diverse and not boring is a hard problem.

There are nn singers and mm songs to be performed. Each singer has several songs in their repertoire, and some of them can be sung in different languages. For a program to be complete, each participant must sing at least one song, and each song must be sung at least once. For a program to be not boring, each participant may use each language at most once, and each song may be sung in each language at most once.

Given the repertoire of each singer, make a complete and not boring program, or determine that it is impossible.

Input

The first line contains three integers nn, mm, and kk: the number of singers, the number of songs, and the number of possible acts, respectively (1≤n,m≤10001 \le n, m \le 1000, 1≤k≤100001 \le k \le 10000).

Each of the next kk lines contains three integers pip_i, sis_i, and lil_i (1≤pi≤n1 \le p_i \le n, 1≤si≤m1 \le s_i \le m, 1≤li≤k1 \le l_i \le k). This means participant pip_i can sing song sis_i in language lil_i.

Output

If it is impossible to make a complete and not boring program, print a single line containing -1.

Otherwise, print the number of songs to be performed, tt, on the first line. On the second line, print tt distinct integers between 1 and kk: the repertoire entries to include in the program. The entries are numbered from 1 in the order they appear in the input, and they may be printed in any order. If there are several valid answers, print any of them.

Examples3

  1. Example 1

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

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

    Input
    2 3 4
    1 1 1
    1 2 1
    2 2 2
    2 3 2
    
    Expected output
    -1