Problem:
Score : 500 points

Problem Statement

We have a grid with H horizontal rows and W vertical columns. Let (i,j) denote the square at the i-th row from the top and the j-th column from the left.

The square (i, j) has two numbers A_{ij} and B_{ij} written on it.

First, for each square, Takahashi paints one of the written numbers red and the other blue.

Then, he travels from the square (1, 1) to the square (H, W). In one move, he can move from a square (i, j) to the square (i+1, j) or the square (i, j+1). He must not leave the grid.

Let the unbalancedness be the absolute difference of the sum of red numbers and the sum of blue numbers written on the squares along Takahashi's path, including the squares (1, 1) and (H, W).

Takahashi wants to make the unbalancedness as small as possible by appropriately painting the grid and traveling on it.

Find the minimum unbalancedness possible.

Constraints

2 \leq H \leq 80

2 \leq W \leq 80

0 \leq A_{ij} \leq 80

0 \leq B_{ij} \leq 80

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W
A_{11} A_{12} \ldots A_{1W}
:
A_{H1} A_{H2} \ldots A_{HW}
B_{11} B_{12} \ldots B_{1W}
:
B_{H1} B_{H2} \ldots B_{HW}

Output

Print the minimum unbalancedness possible.

Sample Input 1

2 2
1 2
3 4
3 4
2 1

Sample Output 1

0

By painting the grid and traveling on it as shown in the figure below, the sum of red numbers and the sum of blue numbers are 3+3+1=7 and 1+2+4=7, respectively, for the unbalancedness of 0.

Sample Input 2

2 3
1 10 80
80 10 1
1 2 3
4 5 6

Sample Output 2

2

Solution in Rust (1.15.1) (judge verdict: Time Limit Exceeded)

#[allow(dead_code)]
fn main() {
    let (h, w) = i::u2();
    let mut a = Vec::new();
    let mut b = Vec::new();
    for _ in 0..h {
        a.push(i::iv(w));
    }
    for _ in 0..h {
        b.push(i::iv(w));
    }
    let mut ab = vec![vec![(0, 0); w]; h];
    for y in 0..h {
        for x in 0..w {
            ab[y][x] = (a[y][x], b[y][x]);
        }
    }
    let grid = Grid::new(h, w, ab);

    let max_d = 12801;
    let mut dp = vec![HashSet::new(); h * w];
    let zero = grid.get(0);
    dp[0].insert((zero.0 - zero.1).abs() as usize);
    let mut q = VecDeque::new();
    q.push_front(0);
    while q.len() > 0 {
        let c = q.pop_front().unwrap();
        for n in grid.one_way(c) {
            let ab = grid.get(n);
            let (a, b) = (ab.0, ab.1);
            for i in dp[c].clone() {
                let i = i as i64;
                let k1 = (i + a - b).abs();
                let k2 = (i + b - a).abs();
                if k1 < max_d as i64 + 1 {
                    dp[n].insert(k1 as usize);
                }
                if k2 < max_d as i64 + 1 {
                    dp[n].insert(k2 as usize);
                }
            }
            q.push_back(n);
        }
    }
    for i in 0..max_d + 1 {
        if dp[h * w - 1].contains(&i) {
            println!("{}", i);
            return;
        }
    }
}

#[allow(unused_imports)]
use grid::*;

#[allow(dead_code)]
mod grid {
    #[derive(Debug)]
    pub struct Grid<T> {
        pub h: usize,
        pub w: usize,
        pub map: Vec<T>,
    }

    impl<T: Clone> Grid<T> {
        pub fn new(h: usize, w: usize, map: Vec<Vec<T>>) -> Grid<T> {
            let mut flat = Vec::new();
            for r in map {
                for c in r {
                    flat.push(c);
                }
            }
            Grid {
                h: h,
                w: w,
                map: flat,
            }
        }
        pub fn key(&self, x: usize, y: usize) -> usize {
            y * self.w + x
        }

        pub fn xy(&self, k: usize) -> (usize, usize) {
            (self.x(k), self.y(k))
        }
        pub fn x(&self, k: usize) -> usize {
            k % self.w
        }
        pub fn y(&self, k: usize) -> usize {
            k / self.w
        }

        pub fn get(&self, key: usize) -> &T {
            &self.map[key]
        }

        pub fn set(&mut self, key: usize, value: T) {
            self.map[key] = value;
        }

        pub fn left(&self, key: usize) -> Option<usize> {
            if self.x(key) > 0 {
                Some(key - 1)
            } else {
                None
            }
        }
        pub fn right(&self, key: usize) -> Option<usize> {
            if self.x(key) < self.w - 1 {
                Some(key + 1)
            } else {
                None
            }
        }
        pub fn up(&self, key: usize) -> Option<usize> {
            if self.y(key) > 0 {
                Some(key - self.w)
            } else {
                None
            }
        }
        pub fn down(&self, key: usize) -> Option<usize> {
            if self.y(key) < self.h - 1 {
                Some(key + self.w)
            } else {
                None
            }
        }

        pub fn neighbor(&self, key: usize) -> Vec<usize> {
            vec![
                self.up(key),
                self.down(key),
                self.left(key),
                self.right(key),
            ]
            .iter()
            .flat_map(|i| *i)
            .collect()
        }

        pub fn one_way(&self, key: usize) -> Vec<usize> {
            vec![self.right(key), self.down(key)]
                .iter()
                .flat_map(|i| *i)
                .collect()
        }
    }
}

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::{HashMap, HashSet, VecDeque};
#[allow(unused_imports)]
use std::io::*;
#[allow(unused_imports)]
use std::num::*;
#[allow(unused_imports)]
use std::str::*;

#[allow(dead_code)]
mod i {
    use super::*;

    pub fn read<T: FromStr>() -> T {
        stdin()
            .bytes()
            .map(|c| c.unwrap() as char)
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>()
            .parse::<T>()
            .ok()
            .unwrap()
    }

    pub fn str() -> String {
        read()
    }

    pub fn s() -> Vec<char> {
        str().chars().collect()
    }

    pub fn i() -> i64 {
        read()
    }

    pub fn u() -> usize {
        read()
    }

    pub fn u2() -> (usize, usize) {
        (read(), read())
    }

    pub fn u3() -> (usize, usize, usize) {
        (read(), read(), read())
    }

    pub fn f() -> f64 {
        read()
    }

    pub fn c() -> char {
        read::<String>().pop().unwrap()
    }

    pub fn iv(n: usize) -> Vec<i64> {
        (0..n).map(|_| i()).collect()
    }

    pub fn iv2(n: usize) -> Vec<(i64, i64)> {
        (0..n).map(|_| iv(2)).map(|a| (a[0], a[1])).collect()
    }

    pub fn uv(n: usize) -> Vec<usize> {
        (0..n).map(|_| u()).collect()
    }

    pub fn uv2(n: usize) -> Vec<(usize, usize)> {
        (0..n).map(|_| uv(2)).map(|a| (a[0], a[1])).collect()
    }

    pub fn uv3(n: usize) -> Vec<(usize, usize, usize)> {
        (0..n).map(|_| uv(3)).map(|a| (a[0], a[1], a[2])).collect()
    }

    pub fn fv(n: usize) -> Vec<f64> {
        (0..n).map(|_| f()).collect()
    }

    pub fn cmap(h: usize) -> Vec<Vec<char>> {
        (0..h).map(|_| s()).collect()
    }
}