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.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * #
 * @author pttrung
 */
public class D_Round_354_Div2 {

    public static long MOD = 1000000007;
    static int[] X = {-1, 1, 0, 0};
    static int[] Y = {0, 0, -1, 1};

    public static void main(String[] args) throws FileNotFoundException {
        // PrintWriter out = new PrintWriter(new FileOutputStream(new File(
        // "output.txt")));
        PrintWriter out = new PrintWriter(System.out);
        Scanner in = new Scanner();
        int n = in.nextInt();
        int m = in.nextInt();
        String[] data = new String[n];
        for (int i = 0; i < n; i++) {
            data[i] = in.next();
        }
        State start = new State(in.nextInt() - 1, in.nextInt() - 1, 0, 0);
        Point end = new Point(in.nextInt() - 1, in.nextInt() - 1);
        int[][][] dist = new int[n][m][4];
        for (int[][] a : dist) {
            for (int[] b : a) {
                Arrays.fill(b, Integer.MAX_VALUE);
            }
        }
        /*
        «+» 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.
         */
        dist[start.x][start.y][0] = 0;
        PriorityQueue<State> q = new PriorityQueue<>((a, b) -> Integer.compare(a.z, b.z));
        q.add(start);
        while (!q.isEmpty()) {
            State s = q.poll();
            if (s.x == end.x && s.y == end.y) {
                break;
            }
            if (dist[s.x][s.y][s.k] == s.z) {
                char c = data[s.x].charAt(s.y);
                if (c != '*') {
                    for (int i = 0; i < 4; i++) {
                        int x = X[i] + s.x;
                        int y = Y[i] + s.y;

                        if (x >= 0 && x < n && y >= 0 && y < m && data[x].charAt(y) != '*') {
                            int nxt = 0;
                            int cost = -1;
                            for (int j = 0; j < 4; j++) {
                                int v = (s.k + j) % 4;
                                //   System.out.println(get(c, v) + " " + get(data[x].charAt(y), v) + " " + v);
                                if (cost(get(c, v), i) == 1 && cost(get(data[x].charAt(y), v), op(i)) == 1) {
                                    cost = 1 + j;
                                    nxt = v;
                                    break;
                                }
                            }
                            //  System.out.println(cost + " " + s + " " + i);
                            if (cost(get(c, nxt), i) == 1 && cost(get(data[x].charAt(y), nxt), op(i)) == 1 && dist[x][y][nxt] > cost + dist[s.x][s.y][s.k]) {
                                dist[x][y][nxt] = cost + dist[s.x][s.y][s.k];
                                q.add(new State(x, y, dist[x][y][nxt], nxt));
                            }
                        }
                    }
                }
            }
        }
        int result = -1;
        for (int i = 0; i < 4; i++) {
            if (dist[end.x][end.y][i] != Integer.MAX_VALUE) {
                if (result == -1 || result > dist[end.x][end.y][i]) {
                    result = dist[end.x][end.y][i];
                }
            }
        }
        out.println(result);

        out.close();
    }

    static int op(int dir) {
        if (dir == 0 || dir == 1) {
            return 1 - dir;
        } else {
            return (3 - dir) + 2;
        }
    }

    static char get(char c, int dir) {
        /*dir : 
        0 up
        1 down
        2 left
        3 right        
         */
        switch (c) {
            case '+':
                return c;
            case '-':
                if (dir % 2 == 1) {
                    return '|';
                }
                return c;
            case '|':
                if (dir % 2 == 1) {
                    return '-';
                }
                return c;
            case '^':
                if (dir == 0) {
                    return c;
                } else if (dir == 1) {
                    return '>';
                } else if (dir == 2) {
                    return 'v';
                }
                return '<';
            case '>':
                if (dir == 0) {
                    return c;
                } else if (dir == 1) {
                    return 'v';
                } else if (dir == 2) {
                    return '<';
                }
                return '^';
            case '<':
                if (dir == 0) {
                    return c;
                } else if (dir == 1) {
                    return '^';
                } else if (dir == 2) {
                    return '>';
                }
                return 'v';
            case 'v':
                if (dir == 0) {
                    return c;
                } else if (dir == 1) {
                    return '<';
                } else if (dir == 2) {
                    return '^';
                }
                return '>';
            case 'L':
                if (dir == 0) {
                    return c;
                } else if (dir == 1) {
                    return 'U';
                } else if (dir == 2) {
                    return 'R';
                }
                return 'D';
            case 'R':
                if (dir == 0) {
                    return c;
                } else if (dir == 1) {
                    return 'D';
                } else if (dir == 2) {
                    return 'L';
                }
                return 'U';
            case 'U':
                if (dir == 0) {
                    return c;
                } else if (dir == 1) {
                    return 'R';
                } else if (dir == 2) {
                    return 'D';
                }
                return 'L';
            case 'D':
                if (dir == 0) {
                    return c;
                } else if (dir == 1) {
                    return 'L';
                } else if (dir == 2) {
                    return 'U';
                }
                return 'R';
            default:
                return c;
        }
    }

    static int cost(char c, int dir) {
        /*dir : 
        0 up
        1 down
        2 left
        3 right        
         */
        switch (c) {
            case '+':
                return 1;
            case '-':
                if (dir == 2 || dir == 3) {
                    return 1;
                }
                return 2;
            case '|':
                if (dir == 0 || dir == 1) {
                    return 1;
                }
                return 2;
            case '^':
                if (dir == 0) {
                    return 1;
                } else if (dir == 1) {
                    return 3;
                } else if (dir == 2) {
                    return 4;
                }
                return 2;
            case '>':
                if (dir == 0) {
                    return 4;
                } else if (dir == 1) {
                    return 2;
                } else if (dir == 2) {
                    return 3;
                }
                return 1;
            case '<':
                if (dir == 0) {
                    return 2;
                } else if (dir == 1) {
                    return 4;
                } else if (dir == 2) {
                    return 1;
                }
                return 3;
            case 'v':
                if (dir == 0) {
                    return 3;
                } else if (dir == 1) {
                    return 1;
                } else if (dir == 2) {
                    return 2;
                }
                return 4;
            case 'L':
                if (dir == 2) {
                    return 2;
                }
                return 1;
            case 'R':
                if (dir == 3) {
                    return 2;
                }
                return 1;
            case 'U':
                if (dir == 0) {
                    return 2;
                }
                return 1;
            case 'D':
                if (dir == 1) {
                    return 2;
                }
                return 1;
            default:
                return 0;
        }
    }

    static class State {

        int x, y, z, k;

        public State(int x, int y, int z, int k) {
            this.x = x;
            this.y = y;
            this.z = z;
            this.k = k;
        }

        @Override
        public String toString() {
            return "State{" + "x=" + x + ", y=" + y + ", z=" + z + ", k=" + k + '}';
        }

    }

    public static int[] KMP(String val) {
        int i = 0;
        int j = -1;
        int[] result = new int[val.length() + 1];
        result[0] = -1;
        while (i < val.length()) {
            while (j >= 0 && val.charAt(j) != val.charAt(i)) {
                j = result[j];
            }
            j++;
            i++;
            result[i] = j;
        }
        return result;

    }

    public static boolean nextPer(int[] data) {
        int i = data.length - 1;
        while (i > 0 && data[i] < data[i - 1]) {
            i--;
        }
        if (i == 0) {
            return false;
        }
        int j = data.length - 1;
        while (data[j] < data[i - 1]) {
            j--;
        }
        int temp = data[i - 1];
        data[i - 1] = data[j];
        data[j] = temp;
        Arrays.sort(data, i, data.length);
        return true;
    }

    public static int digit(long n) {
        int result = 0;
        while (n > 0) {
            n /= 10;
            result++;
        }
        return result;
    }

    public static double dist(long a, long b, long x, long y) {
        double val = (b - a) * (b - a) + (x - y) * (x - y);
        val = Math.sqrt(val);
        double other = x * x + a * a;
        other = Math.sqrt(other);
        return val + other;

    }

    public static class Point implements Comparable<Point> {

        int x, y;

        public Point(int start, int end) {
            this.x = start;
            this.y = end;
        }

        @Override
        public int hashCode() {
            int hash = 5;
            hash = 47 * hash + this.x;
            hash = 47 * hash + this.y;
            return hash;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Point other = (Point) obj;
            if (this.x != other.x) {
                return false;
            }
            if (this.y != other.y) {
                return false;
            }
            return true;
        }

        @Override
        public int compareTo(Point o) {
            return Integer.compare(x, o.x);
        }
    }

    public static class FT {

        long[] data;

        FT(int n) {
            data = new long[n];
        }

        public void update(int index, long value) {
            while (index < data.length) {
                data[index] += value;
                index += (index & (-index));
            }
        }

        public long get(int index) {
            long result = 0;
            while (index > 0) {
                result += data[index];
                index -= (index & (-index));
            }
            return result;

        }
    }

    public static long gcd(long a, long b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }

    public static long pow(long a, long b, long MOD) {
        if (b == 0) {
            return 1;
        }
        if (b == 1) {
            return a;
        }
        long val = pow(a, b / 2, MOD);
        if (b % 2 == 0) {
            return val * val % MOD;
        } else {
            return val * (val * a % MOD) % MOD;

        }
    }

    static class Scanner {

        BufferedReader br;
        StringTokenizer st;

        public Scanner() throws FileNotFoundException {
            // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
            br = new BufferedReader(new InputStreamReader(System.in));
            //  br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
        }

        public String next() {

            while (st == null || !st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                    throw new RuntimeException();
                }
            }
            return st.nextToken();
        }

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

        public int nextInt() {
            return Integer.parseInt(next());
        }

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

        public String nextLine() {
            st = null;
            try {
                return br.readLine();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }

        public boolean endLine() {
            try {
                String next = br.readLine();
                while (next != null && next.trim().isEmpty()) {
                    next = br.readLine();
                }
                if (next == null) {
                    return true;
                }
                st = new StringTokenizer(next);
                return st.hasMoreTokens();
            } catch (Exception e) {
                throw new RuntimeException();
            }
        }
    }
}