R,C = map(int,input().split()) sy,sx = map(int,input().split()) gy,gx= map(int,input().split()) C = [input()for _ in range(C)] sy -= 1;sx -= 1;gy -= 1;gx -= 1 dist = [[2**60]*R for _ in range(C)] dist[sy][sx] = 0 from collections import deque que = deque([(sy,sx)]) d = [-1,1] visited = [[0]*R for _ in range(C)] visited[sy][sx] = 1 while que: u = que.popleft() y,x = u for i in range(2): yy = y+d[i] xx = x+d[i] if 0 <= yy < C: dist[yy][x] = min(dist[yy][x],dist[y][x]+1) if visited[yy][x] == 0: que.append((yy,x)) visited[yy][x] = 1 if 0 <= xx < R: dist[y][xx] = min(dist[y][xx],dist[y][x]+1) if visited[y][xx] == 0: que.append((y,xx)) visited[y][xx] = 1 print(dist[gy][gx])