Problem: Theseus has just arrived to Crete to fight Minotaur. He found a labyrinth that has a form of a rectangular field of size n × m and consists of blocks of size 1 × 1.

Each block of the labyrinth has a button that rotates all blocks 90 degrees clockwise. Each block rotates around its center and doesn't change its position in the labyrinth. Also, each block has some number of doors (possibly none). In one minute, Theseus can either push the button in order to rotate all the blocks 90 degrees clockwise or pass to the neighbouring block. Theseus can go from block A to some neighbouring block B only if block A has a door that leads to block B and block B has a door that leads to block A.

Theseus found an entrance to labyrinth and is now located in block (xT, yT) — the block in the row xT and column yT. Theseus know that the Minotaur is hiding in block (xM, yM) and wants to know the minimum number of minutes required to get there.

Theseus is a hero, not a programmer, so he asks you to help him.

Input

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in labyrinth, respectively.

Each of the following n lines contains m characters, describing the blocks of the labyrinth. The possible characters are:

  * «+» means this block has 4 doors (one door to each neighbouring block); 
  * «-» means this block has 2 doors — to the left and to the right neighbours; 
  * «|» means this block has 2 doors — to the top and to the bottom neighbours; 
  * «^» means this block has 1 door — to the top neighbour; 
  * «>» means this block has 1 door — to the right neighbour; 
  * «<» means this block has 1 door — to the left neighbour; 
  * «v» means this block has 1 door — to the bottom neighbour;
  * «L» means this block has 3 doors — to all neighbours except left one; 
  * «R» means this block has 3 doors — to all neighbours except right one; 
  * «U» means this block has 3 doors — to all neighbours except top one; 
  * «D» means this block has 3 doors — to all neighbours except bottom one;
  * «*» means this block is a wall and has no doors. 



Left, right, top and bottom are defined from representing labyrinth as a table, where rows are numbered from 1 to n from top to bottom and columns are numbered from 1 to m from left to right.

Next line contains two integers — coordinates of the block (xT, yT) (1 ≤ xT ≤ n, 1 ≤ yT ≤ m), where Theseus is initially located.

Last line contains two integers — coordinates of the block (xM, yM) (1 ≤ xM ≤ n, 1 ≤ yM ≤ m), where Minotaur hides.

It's guaranteed that both the block where Theseus starts and the block where Minotaur is hiding have at least one door. Theseus and Minotaur may be initially located at the same block.

Output

If Theseus is not able to get to Minotaur, then print -1 in the only line of the output. Otherwise, print the minimum number of minutes required to get to the block where Minotaur is hiding.

Examples

Input

2 2
+*
*U
1 1
2 2


Output

-1

Input

2 3
&lt;&gt;&lt;
&gt;&lt;&gt;
1 1
2 1


Output

4

Note

Assume that Theseus starts at the block (xT, yT) at the moment 0.

Solution (in JAVA):

Solution:
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Scanner;

public class Main {
	MyScanner sc = new MyScanner();
	Scanner sc2 = new Scanner(System.in);
	long start = System.currentTimeMillis();
	long fin = System.currentTimeMillis();
	final int MOD = 1000000007;
	int[] dx = { 1, 0, 0, -1 };
	int[] dy = { 0, 1, -1, 0 };

	char[] d1 = { '-', '|' };
	char[] d2 = { '^', '>', 'v', '<' };
	char[] d3 = { 'L', 'U', 'R', 'D' };

	Map<Character, Integer> map = new HashMap<Character, Integer>() {
		{
			put('*', Integer.valueOf("0000", 2));
		}
		{
			put('+', Integer.valueOf("1111", 2));
		}
		{
			put('-', Integer.valueOf("0101", 2));
		}
		{
			put('|', Integer.valueOf("1010", 2));
		}
		{
			put('^', Integer.valueOf("0010", 2));
		}
		{
			put('>', Integer.valueOf("0001", 2));
		}
		{
			put('v', Integer.valueOf("1000", 2));
		}
		{
			put('<', Integer.valueOf("0100", 2));
		}
		{
			put('L', Integer.valueOf("1011", 2));
		}
		{
			put('U', Integer.valueOf("1101", 2));
		}
		{
			put('R', Integer.valueOf("1110", 2));
		}
		{
			put('D', Integer.valueOf("0111", 2));
		}
	};

	void run() {
		int h = sc.nextInt();
		int w = sc.nextInt();
		char[][] f = sc.nextCharField(h, w);
		int[][][] c = new int[h][w][4];
		for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) Arrays.fill(c[i][j], Integer.MAX_VALUE / 10);
		int th = sc.nextInt() - 1;
		int tw = sc.nextInt() - 1;
		int mh = sc.nextInt() - 1;
		int mw = sc.nextInt() - 1;
		c[th][tw][0] = 0;
		LinkedList<Pos> que = new LinkedList<>();
		que.add(new Pos(th, tw, 0));
		int res = Integer.MAX_VALUE / 10;
		while (!que.isEmpty()) {
			int size = que.size();
			for (int i = 0; i < size; i++) {
				Pos now = que.poll();
				if (now.nh == mh && now.nw == mw)
					res = Math.min(res, c[now.nh][now.nw][now.deg]);
				// up
				if (inner(now.nh - 1, now.nw, h, w)) {
					boolean from = false;
					boolean to = false;
					if (!(f[now.nh][now.nw] == '*' || f[now.nh - 1][now.nw] == '*')) {
						if (f[now.nh][now.nw] == '+')
							from = true;
						if (f[now.nh - 1][now.nw] == '+')
							to = true;
						int deg = -1;
						// from check
						deg = getDirection(d1, f[now.nh][now.nw], now.deg);
						if (deg != -1)
							if ((deg & 1 << 1) > 0)
								from |= true;
						deg = getDirection(d2, f[now.nh][now.nw], now.deg);
						if (deg != -1)
							if ((deg & 1 << 1) > 0)
								from |= true;
						deg = getDirection(d3, f[now.nh][now.nw], now.deg);
						if (deg != -1)
							if ((deg & 1 << 1) > 0)
								from |= true;

						// to check
						deg = getDirection(d1, f[now.nh - 1][now.nw], now.deg);
						if (deg != -1)
							if ((deg & 1 << 3) > 0)
								to |= true;
						deg = getDirection(d2, f[now.nh - 1][now.nw], now.deg);
						if (deg != -1)
							if ((deg & 1 << 3) > 0)
								to |= true;
						deg = getDirection(d3, f[now.nh - 1][now.nw], now.deg);
						if (deg != -1)
							if ((deg & 1 << 3) > 0)
								to |= true;

						if (from && to && c[now.nh - 1][now.nw][now.deg] > c[now.nh][now.nw][now.deg] + 1) {
							c[now.nh - 1][now.nw][now.deg] = c[now.nh][now.nw][now.deg] + 1;
							que.add(new Pos(now.nh - 1, now.nw, now.deg));
						}
					}
				}
				// right
				if (inner(now.nh, now.nw + 1, h, w)) {
					boolean from = false;
					boolean to = false;
					if (!(f[now.nh][now.nw] == '*' || f[now.nh][now.nw + 1] == '*')) {
						if (f[now.nh][now.nw] == '+')
							from = true;
						if (f[now.nh][now.nw + 1] == '+')
							to = true;
						int deg = -1;
						// from check
						deg = getDirection(d1, f[now.nh][now.nw], now.deg);
						if (deg != -1)
							if ((deg & 1 << 0) > 0)
								from |= true;
						deg = getDirection(d2, f[now.nh][now.nw], now.deg);
						if (deg != -1)
							if ((deg & 1 << 0) > 0)
								from |= true;
						deg = getDirection(d3, f[now.nh][now.nw], now.deg);
						if (deg != -1)
							if ((deg & 1 << 0) > 0)
								from |= true;

						// to check
						deg = getDirection(d1, f[now.nh][now.nw + 1], now.deg);
						if (deg != -1)
							if ((deg & 1 << 2) > 0)
								to |= true;
						deg = getDirection(d2, f[now.nh][now.nw + 1], now.deg);
						if (deg != -1)
							if ((deg & 1 << 2) > 0)
								to |= true;
						deg = getDirection(d3, f[now.nh][now.nw + 1], now.deg);
						if (deg != -1)
							if ((deg & 1 << 2) > 0)
								to |= true;

						if (from && to && c[now.nh][now.nw + 1][now.deg] > c[now.nh][now.nw][now.deg] + 1) {
							c[now.nh][now.nw + 1][now.deg] = c[now.nh][now.nw][now.deg] + 1;
							que.add(new Pos(now.nh, now.nw + 1, now.deg));
						}
					}
				}
				// left
				if (inner(now.nh, now.nw - 1, h, w)) {
					boolean from = false;
					boolean to = false;
					if (!(f[now.nh][now.nw] == '*' || f[now.nh][now.nw - 1] == '*')) {
						if (f[now.nh][now.nw] == '+')
							from = true;
						if (f[now.nh][now.nw - 1] == '+')
							to = true;
						int deg = -1;
						// from check
						deg = getDirection(d1, f[now.nh][now.nw], now.deg);
						if (deg != -1)
							if ((deg & 1 << 2) > 0)
								from |= true;
						deg = getDirection(d2, f[now.nh][now.nw], now.deg);
						if (deg != -1)
							if ((deg & 1 << 2) > 0)
								from |= true;
						deg = getDirection(d3, f[now.nh][now.nw], now.deg);
						if (deg != -1)
							if ((deg & 1 << 2) > 0)
								from |= true;

						// to check
						deg = getDirection(d1, f[now.nh][now.nw - 1], now.deg);
						if (deg != -1)
							if ((deg & 1 << 0) > 0)
								to |= true;
						deg = getDirection(d2, f[now.nh][now.nw - 1], now.deg);
						if (deg != -1)
							if ((deg & 1 << 0) > 0)
								to |= true;
						deg = getDirection(d3, f[now.nh][now.nw - 1], now.deg);
						if (deg != -1)
							if ((deg & 1 << 0) > 0)
								to |= true;

						if (from && to && c[now.nh][now.nw - 1][now.deg] > c[now.nh][now.nw][now.deg] + 1) {
							c[now.nh][now.nw - 1][now.deg] = c[now.nh][now.nw][now.deg] + 1;
							que.add(new Pos(now.nh, now.nw - 1, now.deg));
						}
					}
				}
				// down
				if (inner(now.nh + 1, now.nw, h, w)) {
					boolean from = false;
					boolean to = false;
					if (!(f[now.nh][now.nw] == '*' || f[now.nh + 1][now.nw] == '*')) {
						if (f[now.nh][now.nw] == '+') from = true;
						if (f[now.nh + 1][now.nw] == '+') to = true;
						int deg = -1;
						// from check
						deg = getDirection(d1, f[now.nh][now.nw], now.deg);
						if (deg != -1) if ((deg & 1 << 3) > 0) from |= true;
						deg = getDirection(d2, f[now.nh][now.nw], now.deg);
						if (deg != -1) {
							if ((deg & 1 << 3) > 0) from |= true;
						}
						deg = getDirection(d3, f[now.nh][now.nw], now.deg);
						if (deg != -1)
							if ((deg & 1 << 3) > 0)
								from |= true;

						// to check
						deg = getDirection(d1, f[now.nh + 1][now.nw], now.deg);
						if (deg != -1)
							if ((deg & 1 << 1) > 0)
								to |= true;
						deg = getDirection(d2, f[now.nh + 1][now.nw], now.deg); 
						if (deg != -1) if ((deg & 1 << 1) > 0) to |= true;
						deg = getDirection(d3, f[now.nh + 1][now.nw], now.deg);
						if (deg != -1)
							if ((deg & 1 << 1) > 0)
								to |= true;
						if (from && to && c[now.nh + 1][now.nw][now.deg] > c[now.nh][now.nw][now.deg] + 1) {
							c[now.nh + 1][now.nw][now.deg] = c[now.nh][now.nw][now.deg] + 1;
							que.add(new Pos(now.nh + 1, now.nw, now.deg));
						}
					}
				}
				// rotate
				if (c[now.nh][now.nw][(now.deg + 1) % 4] > c[now.nh][now.nw][now.deg] + 1) {
					c[now.nh][now.nw][(now.deg + 1) % 4] = c[now.nh][now.nw][now.deg] + 1;
					que.add(new Pos(now.nh, now.nw, (now.deg + 1) % 4));
				}
			}
		}
		System.out.println(res == Integer.MAX_VALUE / 10 ? -1 : res);
	}

	class Pos {
		int nh;
		int nw;
		int deg;

		public Pos(int nh, int nw, int deg) {
			super();
			this.nh = nh;
			this.nw = nw;
			this.deg = deg;
		}
		
		void show() {
			System.out.println("h = " + this.nh +" w = " + this.nw +" deg = " + this.deg);
		}
	}

	int getDirection(char[] c, char d, int rotate) {
		int f = -1;
		for (int i = 0; i < c.length; i++) if (c[i] == d) f = i;
		if (f == -1)
			return f;
		return map.get(c[(f + rotate) % c.length]);
	}

	public static void main(String[] args) {
		new Main().run();
	}

	void debug(Object... o) {
		System.out.println(Arrays.deepToString(o));
	}

	void debug2(char[][] array) {
		for (int i = 0; i < array.length; i++) {
			for (int j = 0; j < array[i].length; j++) {
				System.out.print(array[i][j]);
			}
			System.out.println();
		}
	}

	boolean inner(int h, int w, int limH, int limW) {
		return 0 <= h && h < limH && 0 <= w && w < limW;
	}

	void swap(int[] x, int a, int b) {
		int tmp = x[a];
		x[a] = x[b];
		x[b] = tmp;
	}

	// find minimum i (k <= a[i])
	int lower_bound(int a[], int k) {
		int l = -1;
		int r = a.length;
		while (r - l > 1) {
			int mid = (l + r) / 2;
			if (k <= a[mid])
				r = mid;
			else
				l = mid;
		}
		// r = l + 1
		return r;
	}

	// find minimum i (k < a[i])
	int upper_bound(int a[], int k) {
		int l = -1;
		int r = a.length;
		while (r - l > 1) {
			int mid = (l + r) / 2;
			if (k < a[mid])
				r = mid;
			else
				l = mid;
		}
		// r = l + 1
		return r;
	}

	long gcd(long a, long b) {
		return a % b == 0 ? b : gcd(b, a % b);
	}

	long lcm(long a, long b) {
		return a * b / gcd(a, b);
	}

	boolean palindrome(String s) {
		for (int i = 0; i < s.length() / 2; i++) {
			if (s.charAt(i) != s.charAt(s.length() - 1 - i)) {
				return false;
			}
		}
		return true;
	}

	class MyScanner {
		int nextInt() {
			try {
				int c = System.in.read();
				while (c != '-' && (c < '0' || '9' < c))
					c = System.in.read();
				if (c == '-')
					return -nextInt();
				int res = 0;
				do {
					res *= 10;
					res += c - '0';
					c = System.in.read();
				} while ('0' <= c && c <= '9');
				return res;
			} catch (Exception e) {
				return -1;
			}
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		String next() {
			try {
				StringBuilder res = new StringBuilder("");
				int c = System.in.read();
				while (Character.isWhitespace(c))
					c = System.in.read();
				do {
					res.append((char) c);
				} while (!Character.isWhitespace(c = System.in.read()));
				return res.toString();
			} catch (Exception e) {
				return null;
			}
		}

		int[] nextIntArray(int n) {
			int[] in = new int[n];
			for (int i = 0; i < n; i++) {
				in[i] = nextInt();
			}
			return in;
		}

		int[][] nextInt2dArray(int n, int m) {
			int[][] in = new int[n][m];
			for (int i = 0; i < n; i++) {
				in[i] = nextIntArray(m);
			}
			return in;
		}

		double[] nextDoubleArray(int n) {
			double[] in = new double[n];
			for (int i = 0; i < n; i++) {
				in[i] = nextDouble();
			}
			return in;
		}

		long[] nextLongArray(int n) {
			long[] in = new long[n];
			for (int i = 0; i < n; i++) {
				in[i] = nextLong();
			}
			return in;
		}

		char[][] nextCharField(int n, int m) {
			char[][] in = new char[n][m];
			for (int i = 0; i < n; i++) {
				String s = sc.next();
				for (int j = 0; j < m; j++) {
					in[i][j] = s.charAt(j);
				}
			}
			return in;
		}
	}
}