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.io.*;
import java.util.*;

public class TheseusAndLabyrinth {

	private class Block {
	    int rotation;
	    int r;
	    int c;
	    int stepsSoFar;
	    
	    /* hasDoor[d] = true IFF this block has a door facing direction d (assuming the block hasn't been rotated) 
	     * where d is an element of the set {0,1,2,3} standing for {N,E,S,W} respectively. */ 
	    boolean[] hasDoor;

	    Block(int rotation, int row, int col, int stepsSoFar) {
	        this.rotation = rotation;
	        this.stepsSoFar = stepsSoFar;
	        this.r = row;
	        this.c = col;
	        this.hasDoor = new boolean[4];
	    }
	    
	    Block(int rotation, int row, int col, int stepsSoFar, boolean doorNorth, boolean doorEast, boolean doorSouth, boolean doorWest) {
	    	this(rotation, row, col, stepsSoFar);
	    	this.hasDoor[0] = doorNorth;
	    	this.hasDoor[1] = doorEast;
	    	this.hasDoor[2] = doorSouth;
	    	this.hasDoor[3] = doorWest;
	    }

		@Override
		public String toString() {
			return String.format("((%s, %s); rotation: %s; stepsSoFar: %s; hasDoor: %s)", this.r, this.c, this.rotation, this.stepsSoFar, Arrays.toString(this.hasDoor));
		}
	    
	    
	}
	
	private int n, m;
	private boolean[][][] hasDoor;
	private Block[][] blockArr;

    private static final int UP = 0;
    private static final int RIGHT = 1;
    private static final int DOWN = 2;
    private static final int LEFT = 3;
	
	public TheseusAndLabyrinth() {
		try {
			BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
			String line = rd.readLine();
			
			StringTokenizer tokenizer = new StringTokenizer(line);
			this.n = Integer.parseInt(tokenizer.nextToken());
			this.m = Integer.parseInt(tokenizer.nextToken());
			
			/* hasDoor[d][r][c] = true IFF the room located at row r and col c has a door facing direction d, 
			 * where d is an element of the set {0,1,2,3} which stands for {north, east, south, west} respectively. 
			 * Via built-in Java default, all elements of this array are auto-initialized to false. */
			this.hasDoor = new boolean[4][n][m];
			this.blockArr = new Block[n][m];
			
			for (int r = 0; r < n; r++) {
	            line = rd.readLine();
	            
	            for (int c = 0; c < m; c++) {
	            	
	                char ch = line.charAt(c);
	                switch (ch) {
	                    case '+':
	                        hasDoor[0][r][c] = true;
	                        hasDoor[1][r][c] = true;
	                        hasDoor[2][r][c] = true;
	                        hasDoor[3][r][c] = true;
	                        this.blockArr[r][c] = new Block(0, r, c, 0, true, true, true, true);
	                        break;
	                    case '-':
	                        hasDoor[1][r][c] = true;
	                        hasDoor[3][r][c] = true;
	                        this.blockArr[r][c] = new Block(0, r, c, 0, false, true, false, true);
	                        break;
	                    case '|':
	                        hasDoor[0][r][c] = true;
	                        hasDoor[2][r][c] = true;
	                        this.blockArr[r][c] = new Block(0, r, c, 0, true, false, true, false);
	                        break;
	                    case '^':
	                        hasDoor[0][r][c] = true;
	                        this.blockArr[r][c] = new Block(0, r, c, 0, true, false, false, false);
	                        break;
	                    case '>':
	                        hasDoor[1][r][c] = true;
	                        this.blockArr[r][c] = new Block(0, r, c, 0, false, true, false, false);
	                        break;
	                    case '<':
	                        hasDoor[3][r][c] = true;
	                        this.blockArr[r][c] = new Block(0, r, c, 0, false, false, false, true);
	                        break;
	                    case 'v':
	                        hasDoor[2][r][c] = true;
	                        this.blockArr[r][c] = new Block(0, r, c, 0, false, false, true, false);
	                        break;
	                    case 'L':
	                        hasDoor[0][r][c] = true;
	                        hasDoor[1][r][c] = true;
	                        hasDoor[2][r][c] = true;
	                        this.blockArr[r][c] = new Block(0, r, c, 0, true, true, true, false);
	                        break;
	                    case 'R':
	                        hasDoor[0][r][c] = true;
	                        hasDoor[2][r][c] = true;
	                        hasDoor[3][r][c] = true;
	                        this.blockArr[r][c] = new Block(0, r, c, 0, true, false, true, true);
	                        break;
	                    case 'U':
	                        hasDoor[1][r][c] = true;
	                        hasDoor[2][r][c] = true;
	                        hasDoor[3][r][c] = true;
	                        this.blockArr[r][c] = new Block(0, r, c, 0, false, true, true, true);
	                        break;
	                    case 'D':
	                        hasDoor[0][r][c] = true;
	                        hasDoor[1][r][c] = true;
	                        hasDoor[3][r][c] = true;
	                        this.blockArr[r][c] = new Block(0, r, c, 0, true, true, false, true);
	                        break;
	                    case '*':
	                        // no doors
	                    	this.blockArr[r][c] = new Block(0, r, c, 0, false, false, false, false);
	                    default:
	                        break;
	                }
	                //end switch
	            }
	            //end for c
	        }
			//end for r
			
			line = rd.readLine();
			tokenizer = new StringTokenizer(line);			
			int startRow = Integer.parseInt(tokenizer.nextToken()) - 1;	//the '-1' is needed to convert the given number to its corresponding index.
	        int startCol = Integer.parseInt(tokenizer.nextToken()) - 1;
	        
	        line = rd.readLine();
			tokenizer = new StringTokenizer(line);
	        int goalRow = Integer.parseInt(tokenizer.nextToken()) - 1;
	        int goalCol = Integer.parseInt(tokenizer.nextToken()) - 1;
	        
	        //base case - hero starts in the same row and col as the minotaur.
	        //0 steps needed to reach minotaur.
	        if (startRow == goalRow && startCol == goalCol) {
	        	System.out.println("0");
	        	return;
	        }
	        
	        /* visited[f][r][c] = true IFF hero has visited the room which is: 
	         * 1. located at (r, c), AND
	         * 2. rotated from the original position so that it is now facing direction f. */
	        boolean[][][] visited = new boolean[4][n][m];
	        
	        //Built-in ArrayQueue is better for BFS / DFS than Queue (an interface which LinkedList uses)
	        ArrayDeque<Block> q = new ArrayDeque<>();
//	        Queue<Block> q = new Queue<Block>();
//	        q.add(new Block(0, startRow, startCol, 0));
	        q.add(this.blockArr[startRow][startCol]);
	        
	        //Start BFS
	        while (!q.isEmpty()) {
	            Block b = q.removeFirst();

	            if (visited[b.rotation][b.r][b.c]) continue;
	            visited[b.rotation][b.r][b.c] = true;
	            if (b.r == goalRow && b.c == goalCol) {
	            	System.out.println(b.stepsSoFar);
	            	return;
	            }

	            /* Go thru all possible legal actions that can be performed in a single step, and do BFS. */
	            /* LEGAL ACTION #1. Rotate all blocks 90 degrees right.
	             * (Note: it would take too long to actually rotate ALL blocks. So we'll rotate just the current block
	             * and add it back to the queue. We can rotate other blocks dynamically as the need arises.) 
	             * (Note 2: it's better to create a new instance of a Block, with the rotation settings and hasDoor settings modified
	             * accordingly. This is because we might have to add many instances of the same block (with different rotations)
	             * to the queue. So it's necessary to create COPIES of this block.)*/
	            q.add(new Block((b.rotation + 1) % 4, b.r, b.c, b.stepsSoFar + 1, b.hasDoor[3], b.hasDoor[0], b.hasDoor[1], b.hasDoor[2]));
	            
	            /* LEGAL ACTION #2. If possible, move north. Dynamically create copies of neighboring blocks with the rotation setting
	             * that's equal to the current block's rotation setting. */
	            if (b.r > 0) {	//this means you can move north
	            	Block northNeighbor = this.blockArr[b.r - 1][b.c];	//retrieve the neighbor
	            	Block northNeighborCopy = new Block(b.rotation, b.r - 1, b.c, b.stepsSoFar + 1,
	            			northNeighbor.hasDoor[(0 - b.rotation + 4) % 4],
	            			northNeighbor.hasDoor[(1 - b.rotation + 4) % 4],
	            			northNeighbor.hasDoor[(2 - b.rotation + 4) % 4],
	            			northNeighbor.hasDoor[(3 - b.rotation + 4) % 4]);
	            	if (doorsAligned(b, northNeighborCopy, UP, DOWN)) q.addLast(northNeighborCopy);
	            }
	            
	            /* LEGAL ACTION #3. If possible, move east. */
	            if (b.c < m-1) {
	            	Block eastNeighbor = this.blockArr[b.r][b.c + 1];	//retrieve the neighbor
	            	Block eastNeighborCopy = new Block(b.rotation, b.r, b.c + 1, b.stepsSoFar + 1,
	            			eastNeighbor.hasDoor[(0 - b.rotation + 4) % 4],
	            			eastNeighbor.hasDoor[(1 - b.rotation + 4) % 4],
	            			eastNeighbor.hasDoor[(2 - b.rotation + 4) % 4],
	            			eastNeighbor.hasDoor[(3 - b.rotation + 4) % 4]);
	            	if (doorsAligned(b, eastNeighborCopy, RIGHT, LEFT)) q.addLast(eastNeighborCopy);
	            }
	            
	            /* LEGAL ACTION #4. If possible, move south. */
	            if (b.r < n-1) {
	            	Block southNeighbor = this.blockArr[b.r + 1][b.c];	//retrieve the neighbor
	            	Block southNeighborCopy = new Block(b.rotation, b.r + 1, b.c, b.stepsSoFar + 1,
	            			southNeighbor.hasDoor[(0 - b.rotation + 4) % 4],
	            			southNeighbor.hasDoor[(1 - b.rotation + 4) % 4],
	            			southNeighbor.hasDoor[(2 - b.rotation + 4) % 4],
	            			southNeighbor.hasDoor[(3 - b.rotation + 4) % 4]);
	            	if (doorsAligned(b, southNeighborCopy, DOWN, UP)) q.addLast(southNeighborCopy);
	            }
	            
	            /* LEGAL ACTION #5. If possible, move west. */
	            if (b.c > 0) {
	            	Block westNeighbor = this.blockArr[b.r][b.c - 1];	//retrieve the neighbor
	            	Block westNeighborCopy = new Block(b.rotation, b.r, b.c - 1, b.stepsSoFar + 1,
	            			westNeighbor.hasDoor[(0 - b.rotation + 4) % 4],
	            			westNeighbor.hasDoor[(1 - b.rotation + 4) % 4],
	            			westNeighbor.hasDoor[(2 - b.rotation + 4) % 4],
	            			westNeighbor.hasDoor[(3 - b.rotation + 4) % 4]);
	            	if (doorsAligned(b, westNeighborCopy, LEFT, RIGHT)) q.addLast(westNeighborCopy);
	            }
//
//	            Block goLeft = new Block(b.rotation, b.y, b.x - 1, b.steps + 1);
//	            if (b.x > 0 && doors(b, goLeft, LEFT, RIGHT)) q.addLast(goLeft);
//
//	            Block goDown = new Block(b.rotation, b.y + 1, b.x, b.steps + 1);
//	            if (b.y < n-1 && doors(b, goDown, DOWN, UP)) q.addLast(goDown);
//
//	            Block goRight = new Block(b.rotation, b.y, b.x + 1, b.steps + 1);
//	            if (b.x < m-1 && doors(b, goRight, RIGHT, LEFT)) q.addLast(goRight);
	        }
	        
	        //If we get this far, impossible to reach minotaur
	        System.out.println("-1");
	        
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}
	
	
	
	private boolean doorsAligned(Block origin, Block target, int originDirection, int targetDirection) {
//		int modRotOrigin = (requiredOriginDoor - origin.rotation + 4) % 4;
//		int modRotTarget = (requiredTargetDoor - target.rotation + 4) % 4;
//		return (this.hasDoor[modRotOrigin][origin.r][origin.c] && this.hasDoor[modRotTarget][target.r][target.c]);
		return origin.hasDoor[originDirection] && target.hasDoor[targetDirection];
	}
	
    public static void main(String[] args) {
        new TheseusAndLabyrinth();
    }

}