fn read_ints() -> Vec<i32> {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    let s = s.trim_right().to_owned();
    let ws = s.split_whitespace();
    let mut ints = Vec::new();
    for num_s in ws {
        ints.push(num_s.parse().unwrap());
    }
    ints
}

struct ThreeTensor<T> {
    vec: Vec<T>,
    size1: usize,
    size2: usize,
    size3: usize,
}

impl<T: Clone + Copy> ThreeTensor<T> {
    fn new(size1: usize, size2: usize, size3: usize, init: T) -> Self {
        let mut vec = Vec::new();
        vec.resize(size1 * size2 * size3, init);
        ThreeTensor {
            //vec, size1, size2, size3
            vec: vec,
            size1: size1,
            size2: size2,
            size3: size3,
        }
    }

    fn at(&self, i: usize, j: usize, k: usize) -> T {
        self.vec[i * self.size2 * self.size3 + j * self.size3 + k]
    }

    fn set(&mut self, i: usize, j: usize, k: usize, v: T) {
        self.vec[i * self.size2 * self.size3 + j * self.size3 + k] = v;
    }
}

fn main() {
    let ints = read_ints();
    let h = ints[0] as usize;
    let w = ints[1] as usize;
    let mut a = Vec::new();
    let mut b = Vec::new();
    for _ in 0..h {
        a.push(read_ints());
    }
    for _ in 0..h {
        b.push(read_ints());
    }
    let a = a;
    let b = b;
    let mut abs_amb = Vec::new();
    for i in 0..h {
        let mut abs_amb_ = Vec::new();
        for j in 0..w {
            abs_amb_.push((a[i][j] - b[i][j]).abs());
        }
        abs_amb.push(abs_amb_);
    }
    let abs_amb = abs_amb;

    let m = (h + w) * 80;
    let mut bias_cand_tensor = ThreeTensor::new(h, w, m, false);
    for i in 0..h {
        for j in 0..w {
            let abs_amb_ = abs_amb[i][j];
            if i == 0 && j == 0 {
                bias_cand_tensor.set(i, j, abs_amb_ as usize, true);
            } else {
                if i > 0 {
                    for k in 0..m {
                        let new_k = (k as i32 + abs_amb_).abs() as usize;
                        if new_k < m {
                            let new_flg = bias_cand_tensor.at(i, j, new_k)
                                || bias_cand_tensor.at(i - 1, j, k);
                            bias_cand_tensor.set(i, j, new_k, new_flg);
                        }

                        let new_k = (k as i32 - abs_amb_).abs() as usize;
                        if new_k < m {
                            let new_flg = bias_cand_tensor.at(i, j, new_k)
                                || bias_cand_tensor.at(i - 1, j, k);
                            bias_cand_tensor.set(i, j, new_k, new_flg);
                        }
                    }
                }

                if j > 0 {
                    for k in 0..m {
                        let new_k = (k as i32 + abs_amb_).abs() as usize;
                        if new_k < m {
                            let new_flg = bias_cand_tensor.at(i, j, new_k)
                                || bias_cand_tensor.at(i, j - 1, k);
                            bias_cand_tensor.set(i, j, new_k, new_flg);
                        }

                        let new_k = (k as i32 - abs_amb_).abs() as usize;
                        if new_k < m {
                            let new_flg = bias_cand_tensor.at(i, j, new_k)
                                || bias_cand_tensor.at(i, j - 1, k);
                            bias_cand_tensor.set(i, j, new_k, new_flg);
                        }
                    }
                }
            }
        }
    }

    let mut min_bias = 999999;
    for k in 0..m {
        if bias_cand_tensor.at(h - 1, w - 1, k) {
            min_bias = k;
            break;
        }
    }
    println!("{}", min_bias);
}


Measured memory_bytes: 86396