program abc147e
  implicit none
  integer h,w
  integer a(80,80),b(80,80)
  logical ::d(80,80,0:159*80)=.false.
  integer i,j,k,l,n,m
  read(*,*) h,w
  n=(h+w-1)*80
  do i=1,h
     read(*,*) a(i,1:w)
  end do
  do i=1,h
     read(*,*) b(i,1:w)
     a(i,1:w)=abs(a(i,1:w)-b(i,1:w))
  end do
  
  d(1,1,a(1,1))=.true.
  m=a(1,1)
  do i=1,h
     do j=1,w
        do k=0,m
           if(i/=1) then
              if(d(i-1,j,k)) then
                 d(i,j,k+a(i,j))=.true.
                 d(i,j,abs(k-a(i,j)))=.true.
              end if
           end if
           if(j/=1) then
              if(d(i,j-1,k)) then
                 d(i,j,k+a(i,j))=.true.
                 d(i,j,abs(k-a(i,j))=.true.
              end if
           end if
        end do
     end do
  end do
  do i=0,n
     if(d(h,w,i)) then
        print*,i
        stop
     end if
  end do
end program abc147e


