Under construction

Still being built. Things may change

Chances of Winning

Interview

Time limit1sMemory limit128 MB

Summary
Given results of some games among four teams, count the outcomes of the remaining games where team T ends with strictly more points than every other team.
Level

Easy3 of 10

Topics
Brute force, Implementation, Simulation, Combinatorics
Solved
No attempts yet

Problem

You want to find out the chances that your favourite team becomes the champion of a small tournament.

There are exactly four teams. By the end of the tournament, six games will have been played, with each team playing every other team exactly once. Each game either has one team win and the other lose, or ends in a tie. If a game is not a tie, the winning team is awarded 33 points and the losing team 00. If the game is a tie, each team is awarded 11 point.

Your favourite team becomes the champion only if it finishes with strictly more total points than every other team. In other words, being tied for first place is not good enough.

The tournament is not over yet, but you know the results of every game that has already been played. Considering all possible ways points could be awarded in the remaining, not-yet-played games, determine in how many of those cases your favourite team is the champion.

Input

The first line contains an integer TT, your favourite team (1≤T≤41 \le T \le 4). The second line contains an integer GG, the number of games already played (0≤G≤50 \le G \le 5). Each of the next GG lines describes a played game with four space-separated integers AA, BB, SAS_A, SBS_B (1≤A<B≤41 \le A < B \le 4 and SA,SB≥0S_A, S_B \ge 0). This means team AA (with score SAS_A) played team BB (with score SBS_B): team AA won if SA>SBS_A > S_B, team BB won if SA<SBS_A < S_B, and the game was a tie if SA=SBS_A = S_B. No pair of teams plays twice, so the pairs (A,B)(A, B) given in the input are all distinct.

Output

Output a single integer: the number of ways, over all possible outcomes of the remaining games, in which team TT is the tournament champion.

Examples2

  1. Example 1

    Input
    3
    3
    1 3 7 5
    3 4 0 8
    2 4 2 2
    
    Expected output
    0
    
  2. Example 2

    Input
    3
    4
    1 3 5 7
    3 4 8 0
    2 4 2 2
    1 2 5 5
    
    Expected output
    9