program balanced_path
  implicit none
  integer, parameter :: inf = 1000000000
  integer :: h, w, a(80,80) = 0, b(80) = 0, i, j, k
  logical :: dp(80,80,0:13000) = .false.
  read(*,*) h, w
  do i = 1, h
    read(*,*) a(i,1:w)
  end do
  do i = 1, h
    read(*,*) b(1:w)
    a(i,1:w) = abs(a(i,1:w)-b(1:w))
  end do
  dp(1,1,a(1,1)) = .true.
  do i = 1, h
    do j = 1, w
      if (i > 1) then
        do k = 0, 13000
          if (dp(i-1,j,k)) then
            dp(i,j,abs(k-a(i,j))) = .true.
            dp(i,j,k+a(i,j)) = .true.
          end if
        end do
      end if
      if (j > 1) then
        do k = 0, 13000
          if (dp(i,j-1,k)) then
            dp(i,j,abs(k-a(i,j))) = .true.
            dp(i,j,k+a(i,j)) = .true.
          end if
        end do
      end if
    end do
  end do
  do k = 0, 13000
    if (dp(h,w,k)) then
      write(*,'(i0)') k
      stop
    end if
  end do
end program balanced_path