program abc147e
  implicit none
  integer h,w
  integer,allocatable::a(:,:),b(:,:)
  logical,allocatable::d(:,:,:)
  integer i,j,k,l,m,n
  read(*,*) h,w
  n=(h+w)*80
  allocate(a(h,w),b(h,w),d(h,w,0:n))
  do i=1,h
     read(*,*) (a(i,j),j=1,w)
  end do
  do i=1,h
     read(*,*) (b(i,j),j=1,w)
  end do
  a(:,:)=abs(a(:,:)-b(:,:))
  d(:,:,:)=.false.

  d(1,1,a(1,1))=.true.
  do i=1,h
     do j=1,w
        if(i<h) 
        do l=0,n
           if(j/=1) then
              if(d(j-1,k,l).eqv..true.) then
                 m=l+abs(a(j,k)-b(j,k))
                 d(j,k,m)=.true.
                 m=abs(l-abs(a(j,k)-b(j,k)))
                 d(j,k,m)=.true.
              end if
           end if
           if(k/=1) then
              if(d(j,k-1,l).eqv..true.) then
                 m=l+abs(a(j,k)-b(j,k))
                 d(j,k,m)=.true.
                 m=abs(l-abs(a(j,k)-b(j,k)))
                 d(j,k,m)=.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

