import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Queue;
import java.util.LinkedList;
class Node {
public int x;
public int y;
public Node(int x, int y) {
this.x = x;
this.y = y;
}
}
public class Main {
static int n, m, xs, ys, xt, yt;
static int[][] a, b; // 十字路口的属性
static boolean[][] visited; // 节点是否已经被走过
static int[] direction = new int[]{0, -1, 1}; // 移动方向
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] params = br.readLine().trim().split(" ");
n = Integer.parseInt(params[0]);
m = Integer.parseInt(params[1]);
xs = Integer.parseInt(params[2]);
ys = Integer.parseInt(params[3]);
xt = Integer.parseInt(params[4]);
yt = Integer.parseInt(params[5]);
a = new int[n + 1][m + 1];
b = new int[n + 1][m + 1];
visited = new boolean[n + 1][m + 1];
for(int i = 1; i <= n; i++){
params = br.readLine().trim().split(" ");
for(int j = 1; j <= m; j++)
a[i][j] = Integer.parseInt(params[j - 1]);
}
for(int i = 1; i <= n; i++){
params = br.readLine().trim().split(" ");
for(int j = 1; j <= m; j++)
b[i][j] = Integer.parseInt(params[j - 1]);
}
int cost = 0; // 初始化花费的时间
visited[xs][ys] = true;
System.out.println(bfs(cost));
}
private static int bfs(int cost) {
// 用于存储同一时间点可能到达的节点
Queue queue = new LinkedList<>();
// 先把起点加入队列
queue.offer(new Node(xs, ys));
while(!queue.isEmpty()){
int len = queue.size(); // 当前时刻有len个可能的位置
while(len-- > 0){
Node cur = queue.poll();
// 找到了小团,直接返回耗时
if(cur.x == xt && cur.y == yt) return cost;
// 在当前位置尝试移动
for(int j = 0; j
int remainder = cost % (a[cur.x][cur.y] + b[cur.x][cur.y]);
int x = cur.x, y = cur.y;
if(remainder
// 时间处于[k*aij+k*bij), (k+1)*aij+k*bij)
x += direction[j];
}else{
// 时间处于[(k+1)*aij+k*bij), (k+1)*aij+(k+1)*bij)
y += direction[j];
}
// 移动位置不合法
if(x n || y m) continue;
// 如果下一个位置还没有走过或者当前时刻不进行移动,就往队列中添加节点
if(!visited[x][y] || j == 0){
queue.offer(new Node(x, y));
visited[x][y] = true;
}
}
}
cost ++;
}
return cost;
}
}