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.*;

/**
 * 
 * @author T2P10
 * Verdict: VALID
 */
public class TheseusAndLabyrinth {

	/**
	 * Inner class.
	 * @author T2P10
	 *
	 */
	private class Block {
	    int rotation;	//a block may be rotated 90 degrees to the right, so its direction may change.
	    int r;	//row
	    int c;	//col
	    int stepsSoFar;	//the no. of steps it took hero to get to this block
	    
	    /* hasDoor[d] = true IFF this block has a door facing direction d, 
	     * where d is an element of the set {0,1,2,3} standing for {N,E,S,W} respectively. */ 
	    boolean[] hasDoor;

	    /**
	     * Constructor.
	     * @param rotation
	     * @param row
	     * @param col
	     * @param stepsSoFar
	     */
	    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];
	    }
	    
	    /**
	     * Overloaded constructor.
	     * @param rotation
	     * @param row
	     * @param col
	     * @param stepsSoFar
	     * @param doorNorth
	     * @param doorEast
	     * @param doorSouth
	     * @param doorWest
	     */
	    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));
		} 
	}
	//end private class Block
	
	private int n, m;	//total no. of rows and cols
	private Block[][] blockArr;	//2D array of initial block configurations (where all blocks are unrotated)

    private static final int UP = 0;
    private static final int RIGHT = 1;
    private static final int DOWN = 2;
    private static final int LEFT = 3;
	
    /**
     * Constructor
     */
	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());
			
			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 '+':
	                        this.blockArr[r][c] = new Block(0, r, c, 0, true, true, true, true);
	                        break;
	                    case '-':
	                        this.blockArr[r][c] = new Block(0, r, c, 0, false, true, false, true);
	                        break;
	                    case '|':
	                        this.blockArr[r][c] = new Block(0, r, c, 0, true, false, true, false);
	                        break;
	                    case '^':
	                        this.blockArr[r][c] = new Block(0, r, c, 0, true, false, false, false);
	                        break;
	                    case '>':
	                        this.blockArr[r][c] = new Block(0, r, c, 0, false, true, false, false);
	                        break;
	                    case '<':
	                        this.blockArr[r][c] = new Block(0, r, c, 0, false, false, false, true);
	                        break;
	                    case 'v':
	                        this.blockArr[r][c] = new Block(0, r, c, 0, false, false, true, false);
	                        break;
	                    case 'L':
	                        this.blockArr[r][c] = new Block(0, r, c, 0, true, true, true, false);
	                        break;
	                    case 'R':
	                        this.blockArr[r][c] = new Block(0, r, c, 0, true, false, true, true);
	                        break;
	                    case 'U':
	                        this.blockArr[r][c] = new Block(0, r, c, 0, false, true, true, true);
	                        break;
	                    case 'D':
	                        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<>();

	        q.add(this.blockArr[startRow][startCol]);
	        
	        //Start BFS to find shortest path from hero to minotaur.
	        while (!q.isEmpty()) {
	            Block b = q.removeFirst();

	            //skip this block if it was already visited in this rotation configuration.
	            if (visited[b.rotation][b.r][b.c]) continue;
	            
	            visited[b.rotation][b.r][b.c] = true;
	            
	            //base case. goal reached!
	            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. 
	             * Remember to create the neighbor block copy such that its door positions take into account
	             * the current 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 (unrotated)
	            	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);
	            }
	        }
	        //end while (!q.isEmpty()) {
	        
	        
	        //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) {
		return origin.hasDoor[originDirection] && target.hasDoor[targetDirection];
	}
	
    public static void main(String[] args) {
        new TheseusAndLabyrinth();
    }
}