#include<bits/stdc++.h>
using namespace std;
const int N=1001000;
int n;
char s[N],t[N];
deque<int> deq;
int main(){
	cin>>n;
	scanf("%s%s",s+1,t+1);
	int he=0,ta=1,l=n+1,tag=0,ans=0;
	deq.push_back(n+1);
	for(int i=n;i;--i){
		int tmp=l;
		while(l&&(l>i||s[l]!=t[i])) --l;
		if(!l) return puts("-1")&0;
		if(tmp==l){
			while(!deq.empty()&&deq.back()-tag>=i) deq.pop_back();
			deq.push_back(i+tag);
		}
		else{
			++tag;
			if(l!=i) ans=max(ans,(int)deq.size()),deq.push_front(l+tag);
		}
	}
	cout<<ans<<endl;
	return 0;
}
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define CLR(x) memset((x), false, sizeof(x))
const int MAXD = 12801;
const int MAXN = 81;
int  A[2][MAXN][MAXN];
bool   dp[MAXN][MAXN][MAXD];
int H, W;

int solve() {
  CLR(dp);
  dp[0][0][0] = true;
  dp[0][1][0] = true;
  dp[1][0][0] = true;
  for (int i = 1; i <= H; ++i) {
    for (int j = 1; j <= W; ++j) {
      for (int k = 0; k < MAXD; ++k) {
        int d = std::abs(A[0][i][j] - A[1][i][j]);
        int nk1 = k + d;
        if (nk1 < MAXD) {
          dp[i][j][k] |= dp[i-1][j][nk1] | dp[i][j-1][nk1];
        }
        int nk2 = std::abs(k - d);
        dp[i][j][k] |= dp[i-1][j][nk2] | dp[i][j-1][nk2];
      }
    }
  }
  for (int k = 0; k < MAXD; ++k) {
    if (dp[H][W][k]) {
      return k;
    }
  }
  return -1;
}

int main() {
  scanf(" %d%d", &H, &W);
  for (int k = 0; k < 2; ++k) {
    for (int i = 1; i <= H; ++i) {
      for (int j = 1; j <= W; ++j) {
        scanf("%d", &A[k][i][j]);
      }
    }
  }
  printf("%d\n", solve());
  return 0;
}