This page is still under construction.

Parts of this page are still being built. What you see may change.

Matrix

Interview

Time limit2sMemory limit512 MB

Summary
Find row addition counts from 0 to 9 that turn matrix A into B with matching column subtraction counts and the smallest row digit string.
Level

Medium5 of 10

Topics
Matrix, Math, Greedy
Solved
No attempts yet

Problem

You are given two m×nm \times n matrices AA and BB. Matrix BB comes from matrix AA by row-addition operations and column-subtraction operations. One row-addition operation adds 1 to every entry of a row, and one column-subtraction operation subtracts 1 from every entry of a column.

Find the numbers of row-addition operations r1,…,rmr_1, \dots, r_m applied to row 1 through row mm of AA so that all of the following hold.

  • There exist numbers of column-subtraction operations c1,…,cnc_1, \dots, c_n applied to column 1 through column nn of AA such that these row and column operations turn AA into BB.
  • Every count is between 0 and 9 inclusive, that is 0≤ri≤90 \le r_i \le 9 for i=1,…,mi = 1, \dots, m and 0≤cj≤90 \le c_j \le 9 for j=1,…,nj = 1, \dots, n.
  • The value r1…rmr_1 \dots r_m, read as one integer, is as small as possible.

The answer is r1r_1 through rmr_m concatenated in order. If BB cannot be obtained from AA within these limits, the answer is −1-1.

Input

The first line contains two integers mm and nn separated by a space (1≤m≤1001 \le m \le 100, 1≤n≤1001 \le n \le 100).

The next mm lines give matrix AA, from row 1 to row mm. Each of these lines contains nn integers separated by single spaces. The next mm lines give matrix BB in the same format.

Every entry of both matrices is an integer between −1000-1000 and 10001000 inclusive.

Output

Print one line.

If the transformation is possible, print r1r_1 through rmr_m concatenated in order as a digit string of length mm, keeping any leading zeros. Otherwise print −1-1.

Examples3

  1. Example 1

    Input
    2 3
    1 2 3
    4 5 6
    1 0 0
    0 -1 -1
    
    Expected output
    40
    
  2. Example 2

    Input
    3 3
    1 2 3
    4 5 6
    7 8 9
    1 4 7
    2 5 8
    3 6 9
    
    Expected output
    420
    
  3. Example 3

    Input
    2 1
    0
    -9
    9
    9
    
    Expected output
    -1