Under construction

Still being built. Things may change

Win

Time limit2sMemory limit512 MB

Summary
Given a league with at most 20 unplayed games, count the distinct win-loss outcomes of those games where the favorite team finishes in sole first place.
Level

Medium4 of 10

Topics
Brute force, Bit manipulation
Solved
No attempts yet

Problem

Song-i is a baseball fan. She wants her favorite team to finish first, so she predicts the results of the remaining games to see where her team stands.

The league has MM games in total. Each game has two teams. There are no draws, so one team always wins and the other loses. The winning team gets 1 point. To take sole first place in the league, a team must have the highest points among all teams, and no other team may have the same points.

Count the number of ways her favorite team can take sole first place. Two orders of playing the games count as one case if the results are the same.

Input

The first line contains three integers NN, MM, and KK, separated by spaces: the number of teams, the number of games, and the team number of Song-i's favorite team. (2≤N≤100 000;(2 \leq N \leq 100\,000; 1≤M≤100 000;1 \leq M \leq 100\,000; 1≤K≤N)1 \leq K \leq N)

Each of the next MM lines describes a game in the form (team 1, team 2, result). Team numbers are integers from 11 to NN, and team 1 and team 2 are always different. The result is 00 if the game has not been played yet, 11 if team 1 won, and 22 if team 2 won. At most 2020 games have not been played yet. Her favorite team may not appear in the input.

All numbers in the input are integers.

Output

Print the number of ways her favorite team can take sole first place after all unplayed games are finished.

Examples1

  1. Example 1

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