Under construction

Still being built. Things may change

World Cup Group Stage

Interview

Time limit2sMemory limit512 MB

Summary
Given a group table with one missing score for team T, find the smallest goal count that lets T finish in the top two, or -1 if none exists up to K.
Level

Medium5 of 10

Topics
Binary search, Implementation, Sorting
Solved
No attempts yet

Problem

The FIFA World Cup is held every four years. Starting with the 2022 Qatar World Cup, teams are divided into groups of four, and each team plays every other team in its group once. Only the top two teams in each group advance to the knockout stage. Group standings are decided by the following criteria, in order:

  1. Points: the team with more points ranks higher. Points depend on the match results: 3 for a win, 1 for a draw, 0 for a loss.
  2. Goal difference: the team with a larger (goals scored - goals conceded) ranks higher.
  3. Goals scored: the team that scored the most goals ranks higher.
  4. Head-to-head: the team with the higher head-to-head rank in the group ranks higher.

Standings are sorted by points first. If points are tied, goal difference decides. If goal difference is also tied, goals scored decides. If that is tied too, head-to-head rank decides.

Teams A, B, C, and D are placed in one group. Their head-to-head ranks are always A first, B second, C third, and D fourth. After all matches, a score table shows how many goals each team scored in each match.

Score table(Team A)(Team B)(Team C)(Team D)
Team A-WABW_{AB}WACW_{AC}WADW_{AD}
Team BWBAW_{BA}-WBCW_{BC}WBDW_{BD}
Team CWCAW_{CA}WCBW_{CB}-WCDW_{CD}
Team DWDAW_{DA}WDBW_{DB}WDCW_{DC}-

WijW_{ij} is the number of goals team ii scored against team jj. Equivalently, WijW_{ij} is the number of goals team jj conceded against team ii.

Score table(Team A)(Team B)(Team C)(Team D)
Team A0231
Team B0020
Team C2003
Team D??020

Table 1

The result of any match can be read from the table. To find the result of Team A against Team B, check row Team A, column (Team B), and row Team B, column (Team A). Here the result is 2:0, a win for Team A.

In the match between Team A and Team D, Team D's score is missing and is marked ??. Team D must score at least 2 goals against Team A to advance.

Given a score table where one score of team TT is missing, find the minimum value of ?? that lets team TT advance. If team TT cannot advance regardless of the value of ??, print -1.

Input

The first line contains the head-to-head rank of team TT and a positive integer KK, separated by a space.

The next 4 lines contain the score table. Each line contains the scores WijW_{ij} separated by spaces.

  • If i=ji=j, the value is given as 0.
  • If i≠ji \neq j:
    • A score that is not missing is given as a non-negative integer.
    • The missing score ?? is given as -1.

The second line is the scores of Team A, the third line Team B, the fourth line Team C, and the fifth line Team D.

The score table always has exactly one missing score, and team TT always has a missing score in one of its matches.

Output

Print the minimum value of ?? that lets team TT advance.

However, if the minimum is greater than KK, or if team TT cannot advance regardless of the value of ??, print -1.

Constraints

  • 1 000≤K≤10121\,000 \le K \le 10^{12}
  • 0≤Wij≤K0 \le W_{ij} \le K

Examples4

  1. Example 1

    Input
    4 1000
    0 2 3 1
    0 0 2 0
    2 0 0 3
    -1 0 2 0
    
    Expected output
    2
    
  2. Example 2

    Input
    4 1000
    0 2 1 2
    0 0 2 3
    1 1 0 2
    -1 1 0 0
    
    Expected output
    -1
    
  3. Example 3

    Input
    2 1000
    0 2 1 0
    1 0 1 -1
    1 0 0 1
    1 0 0 0
    
    Expected output
    0
    
  4. Example 4

    Input
    1 1000
    0 -1 1 1
    0 0 5 3
    0 0 0 2
    0 1 1 0
    
    Expected output
    0