Vasilisa the Wise from the Kingdom of Far Far Away got a magic box with a secret as a present from her friend Hellawisa the Wise from the Kingdom of A Little Closer. However, Vasilisa the Wise does not know what the box's secret is, since she cannot open it again. She hopes that you will help her one more time with that.
The box's lock looks as follows: it contains 4 identical deepenings for gems as a 2 × 2 square, and some integer numbers are written at the lock's edge near the deepenings. The example of a lock is given on the picture below.
The box is accompanied with 9 gems. Their shapes match the deepenings' shapes and each gem contains one number from 1 to 9 (each number is written on exactly one gem). The box will only open after it is decorated with gems correctly: that is, each deepening in the lock should be filled with exactly one gem. Also, the sums of numbers in the square's rows, columns and two diagonals of the square should match the numbers written at the lock's edge. For example, the above lock will open if we fill the deepenings with gems with numbers as is shown on the picture below.
Now Vasilisa the Wise wants to define, given the numbers on the box's lock, which gems she should put in the deepenings to open the box. Help Vasilisa to solve this challenging task.
Input
The input contains numbers written on the edges of the lock of the box. The first line contains space-separated integers r1 and r2 that define the required sums of numbers in the rows of the square. The second line contains space-separated integers c1 and c2 that define the required sums of numbers in the columns of the square. The third line contains space-separated integers d1 and d2 that define the required sums of numbers on the main and on the side diagonals of the square (1 ≤ r1, r2, c1, c2, d1, d2 ≤ 20). Correspondence between the above 6 variables and places where they are written is shown on the picture below. For more clarifications please look at the second sample test that demonstrates the example given in the problem statement.
Output
Print the scheme of decorating the box with stones: two lines containing two space-separated integers from 1 to 9. The numbers should be pairwise different. If there is no solution for the given lock, then print the single number "-1" (without the quotes).
If there are several solutions, output any.
Examples
Input
3 7
4 6
5 5
Output
1 2
3 4
Input
11 10
13 8
5 16
Output
4 7
9 1
Input
1 2
3 4
5 6
Output
-1
Input
10 10
10 10
10 10
Output
-1
Note
Pay attention to the last test from the statement: it is impossible to open the box because for that Vasilisa the Wise would need 4 identical gems containing number "5". However, Vasilisa only has one gem with each number from 1 to 9.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to solve the problem. We have a 2x2 grid with cells: top-left (a), top-right (b), bottom-left (c), bottom-right (d). We are given:
row sums: r1 (top row sum: a + b), r2 (bottom row sum: c + d)
column sums: c1 (left column sum: a + c), c2 (right column sum: b + d)
diagonal sums: d1 (main diagonal: a + d), d2 (other diagonal: b + c)
We need to assign numbers 1..9 to a, b, c, d, all distinct, each from 1..9.
Given these constraints, we need to find any solution or -1.
We can solve by brute force: try all permutations of 4 distinct numbers from 1..9 (9P4 = 3024) which is trivial. For each candidate (a,b,c,d) check the sums.
But maybe there is a more direct approach: from sums we can solve for a,b,c,d:
We have linear equations:
(1) a + b = r1
(2) c + d = r2
(3) a + c = c1
(4) b + d = c2
(5) a + d = d1
(6) b + c = d2
We have 4 unknowns, 6 equations. For consistency, the equations must satisfy certain relationships. But we can solve for a,b,c,d in terms of sums.
Adding all row sums: (a+b)+(c+d) = r1+r2 = total sum of all cells = S.
Also S = a+b+c+d.
Similarly, from columns: (a+c)+(b+d) = c1+c2 = S.
And from diagonals: (a+d)+(b+c) = d1+d2 = S.
Thus consistency: r1+r2 = c1+c2 = d1+d2 must hold. But problem constraints may not guarantee that, but if not, no solution.
But we also can solve: From (1)-(4), we can derive:
a = (r1 + c1 - d2)/2? Let's derive systematically.
We have:
a + b = r1 (i)
a + c = c1 (ii)
a + d = d1 (iii)
b + c = d2 (iv)
b + d = c2 (v)
c + d = r2 (vi)
We can solve for a,b,c,d. For example, adding (i) and (vi): (a+b)+(c+d) = r1+r2 => S = r1+r2.
Similarly, from (i) and (ii): we can express b = r1 - a, c = c1 - a. Then (iv): b + c = d2 => (r1 - a) + (c1 - a) = d2 => r1 + c1 - 2a = d2 => a = (r1 + c1 - d2)/2.
Similarly, we can get other formulas. Then check if a is integer, etc.
Then b = r1 - a, c = c1 - a, d = d1 - a (or from other equations). Then verify all sums and distinctness and numbers between 1..9.
Given constraints small, brute force is simplest. But to be safe, we can implement brute force.
We must output two lines: a b on first line, c d on second.
If multiple solutions, any.
Edge case: no solution => -1.
Brute force approach:
For a from 1 to 9:
for b from 1 to 9, b != a:
for c from 1 to 9, c not in {a,b}:
for d from 1 to 9, d not in {a,b,c}:
if a+b == r1 and c+d == r2 and a+c == c1 and b+d == c2 and a+d == d1 and b+c == d2: output and exit.
If loops end, print -1.
Complexity: 987*6 = 3024, fine.
We'll implement.
But careful: Input order: first line r1 r2, second line c1 c2, third line d1 d2. According to picture, r1 is top row, r2 bottom row; c1 left column, c2 right column; d1 main diagonal (top-left to bottom-right), d2 other diagonal (top-right to bottom-left). So our equations correct.
Testing examples:
Example 1:
Input:
3 7
4 6
5 5
Our brute force should find a=1,b=2,c=3,d=4? Check: 1+2=3, 3+4=7, 1+3=4, 2+4=6, 1+4=5, 2+3=5. Yes.
Example 2:
11 10
13 8
5 16
We get 4 7 / 9 1. Check: 4+7=11, 9+1=10, 4+9=13, 7+1=8, 4+1=5, 7+9=16. Yes.
Example 3: 1 2 / 3 4 / 5 6 => no solution.
Example 4: 10 10 / 10 10 / 10 10 => impossible because would need numbers all 5? Actually if a=b=c=d=5, sums 10, but numbers must be distinct, so no solution. Also 5+5=10 but repeats.
So brute force will handle.
But also note that numbers must be from 1 to 9 and distinct. So check.
Edge: maybe some inputs produce solution with numbers outside 1..9? Our loops restrict 1..9. So if a solution exists with number >9 or <1 but still distinct and between 1-9? Not allowed. So we only consider 1..9. So we are fine.
Implementation in Python.
Pseudo:
def solve():
import sys
data = sys.stdin.read().strip().split()
if len(data) != 6: ... but assume exactly.
r1, r2, c1, c2, d1, d2 = map(int, data)
for a in range(1,10):
for b in range(1,10):
if b == a: continue
for c in range(1,10):
if c == a or c == b: continue
for d in range(1,10):
if d == a or d == b or d == c: continue
if (a+b == r1 and c+d == r2 and a+c == c1 and b+d == c2 and a+d == d1 and b+c == d2):
print(a,b)
print(c,d)
return
print(-1)
But we can make it more efficient: loop only over a and b, then compute c and d from equations, and check if they are distinct and 1-9.
But both fine.
I'll implement brute force for clarity.
Let's test quickly mental:
Example 1: r1=3,r2=7,c1=4,c2=6,d1=5,d2=5.
a loop: a=1,b=2 -> a+b=3 OK, then c? Let's compute from a+c=4 => c=3, check d: from c+d=7 => d=4, check b+d=2+4=6 OK, a+d=1+4=5 OK, b+c=2+3=5 OK, distinct and 1-9. Yes.
Works.
Edge: multiple solutions? print any.
We'll implement.
Potential pitfalls: reading input lines with spaces and newlines. Use sys.stdin.read().
One more note: The problem statement says "The first line contains space-separated integers r1 and r2 that define the required sums of numbers in the rows of the square. The second line contains space-separated integers c1 and c2 that define the required sums of numbers in the columns of the square. The third line contains space-separated integers d1 and d2 that define the required sums of numbers on the main and on the side diagonals of the square". So our parsing is correct.
Alright.
Time to code.
But wait: The problem might have an input where sums are like 20, 20, 20, etc. Our brute force still works.
Ok.
We'll produce final answer.
However, I'd like to consider optimization? Not needed.
But ensure we handle situation where there is solution but numbers not in 1..9? Actually problem says each gem contains one number from 1 to 9, so only numbers 1..9. So no.
# brute force over all permutations of four distinct numbers from 1..9
for a in range(1, 10):
for b in range(1, 10):
if b == a:
continue
for c in range(1, 10):
if c == a or c == b:
continue
for d in range(1, 10):
if d == a or d == b or d == c:
continue
if (a + b == r1 and c + d == r2 and
a + c == c1 and b + d == c2 and
a + d == d1 and b + c == d2):
print(a, b)
print(c, d)
return
print(-1)