Problem: You are a resident of Kyoot (oh, well, it’s not a misspelling!) city. All streets there are neatly built on a grid; some streets run in a meridional (north-south) direction and others in a zonal (east-west) direction. The streets that run from north to south are called avenues, whereas those which run from east to west are called drives.

Every avenue and drive in the city is numbered to distinguish one from another. The westernmost avenue is called the 1st avenue. The avenue which lies next to the 1st avenue is the 2nd avenue, and so forth. Similarly, drives are numbered from south to north. The figure below illustrates this situation.

<image>

Figure 1: The Road Map of the Kyoot City

There is an intersection with traffic signals to regulate traffic on each crossing point of an avenue and a drive. Each traffic signal in this city has two lights. One of these lights is colored green, and means “you may go”. The other is red, and means “you must stop here”. If you reached an intersection during the red light (including the case where the light turns to red on your arrival), you must stop and wait there until the light turns to green again. However, you do not have to wait in the case where the light has just turned to green when you arrived there.

Traffic signals are controlled by a computer so that the lights for two different directions always show different colors. Thus if the light for an avenue is green, then the light for a drive must be red, and vice versa. In order to avoid car crashes, they will never be green together. Nor will they be red together, for efficiency. So all the signals at one intersection turn simultaneously; no sooner does one signal turn to red than the other turns to green. Each signal has a prescribed time interval and permanently repeats the cycle.

<image>

Figure 2: Signal and Intersection

By the way, you are planning to visit your friend by car tomorrow. You want to see her as early as possible, so you are going to drive through the shortest route. However, due to existence of the traffic signals, you cannot easily figure out which way to take (the city also has a very sophisticated camera network to prevent crime or violation: the police would surely arrest you if you didn’t stop on the red light!). So you decided to write a program to calculate the shortest possible time to her house, given the town map and the configuration of all traffic signals.

Your car runs one unit distance in one unit time. Time needed to turn left or right, to begin moving, and to stop your car is negligible. You do not need to take other cars into consideration.



Input

The input consists of multiple test cases. Each test case is given in the format below:


w h
dA,1 dA,2 . . . dA,w−1
dD,1 dD,2 . . . dD,h−1
ns1,1 ew1,1 s1,1
.
.
.
nsw,1 eww,1 sw,1
ns1,2 ew1,2 s1,2
.
.
.
nsw,h eww,h sw,h
xs ys
xd yd


Two integers w and h (2 ≤ w, h ≤ 100) in the first line indicate the number of avenues and drives in the city, respectively. The next two lines contain (w - 1) and (h - 1) integers, each of which specifies the distance between two adjacent avenues and drives. It is guaranteed that 2 ≤ dA,i , dD,j ≤ 1, 000.

The next (w × h) lines are the configuration of traffic signals. Three parameters nsi,j, ewi,j and si,j describe the traffic signal (i, j) which stands at the crossing point of the i-th avenue and the j-th drive. nsi,j and ewi,j (1 ≤ nsi,j, ewi,j < 100) are time intervals for which the light for a meridional direction and a zonal direction is green, respectively. si,j is the initial state of the light: 0 indicates the light is green for a meridional direction, 1 for a zonal direction. All the traffic signal have just switched the state to si,j at your departure time.

The last two lines of a case specify the position of your house (xs, ys ) and your friend’s house (xd, yd ), respectively. These coordinates are relative to the traffic signal (0, 0). x-axis is parallel to the drives, and y-axis is parallel to the avenues. x-coordinate increases as you go east, and y-coordinate increases as you go north. You may assume that these positions are on streets and will never coincide with any intersection.

All parameters are integers and separated by a blank character.

The end of input is identified by a line containing two zeros. This is not a part of the input and should not be processed.

Output

For each test case, output a line containing the shortest possible time from your house to your friend’s house.

Example

Input

4 4
2 2 2
2 2 2
99 1 0
1 99 1
1 99 1
1 99 1
1 99 1
1 99 1
1 99 1
99 1 0
99 1 0
1 99 1
1 99 1
1 99 1
1 99 1
1 99 1
1 99 1
99 1 0
1 0
1 6
2 2
10
10
5 5 0
5 5 0
5 5 0
5 5 0
5 0
5 10
0 0


Output

28
25

Solution (in CPP):

Solution:
#include <cstdio>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

const int INF = INT_MAX / 2;

int h, w;
vector<int> dy, dx;
vector<vector<int> > ns, ew, s;

void add(int& y, int& x)
{
    int y2 = 1;
    while(y > 0){
        y -= dy[y2];
        ++ y2;
    }
    int x2 = 1;
    while(x > 0){
        x -= dx[x2];
        ++ x2;
    }

    if(y != 0 || x != 0){
        if(y == 0){
            dx[x2-1] += x;
            dx.insert(dx.begin()+x2, -x);
            for(int i=0; i<h+2; ++i){
                ns[i].insert(ns[i].begin()+x2, 1);
                ew[i].insert(ew[i].begin()+x2, INF);
                s[i].insert(s[i].begin()+x2, 1);
            }
            ++ w;
        }else{
            dy[y2-1] += y;
            dy.insert(dy.begin()+y2, -y);
            ns.insert(ns.begin()+y2, vector<int>(w+2, INF));
            ew.insert(ew.begin()+y2, vector<int>(w+2, 1));
            s.insert(s.begin()+y2, vector<int>(w+2, 0));
            ++ h;
        }
    }

    y = y2;
    x = x2;
}

int main()
{
    for(;;){
        cin >> w >> h;
        if(h == 0)
            return 0;

        dx.assign(w+1, INF);
        dy.assign(h+1, INF);
        for(int i=1; i<w; ++i)
            cin >> dx[i];
        for(int i=1; i<h; ++i)
            cin >> dy[i];

        ns = ew = s = vector<vector<int> >(h+2, vector<int>(w+2, 1));
        for(int i=1; i<=h; ++i){
            for(int j=1; j<=w; ++j){
                cin >> ns[i][j] >> ew[i][j] >> s[i][j];
            }
        }

        int sy, sx, gy, gx;
        cin >> sx >> sy >> gx >> gy;
        int sy0 = sy;
        int sx0 = sx;
        add(sy0, sx0);
        add(gy, gx);
        add(sy, sx);

        vector<vector<vector<int> > > dp(h+2, vector<vector<int> >(w+2, vector<int>(2, INF)));
        for(int i=0; i<h+2; ++i)
            dp[i][0][1] = dp[i][w+1][1] = -1;
        for(int i=0; i<w+2; ++i)
            dp[0][i][0] = dp[h+1][i][0] = -1;

        multimap<int, pair<pair<int, int>, int> > mm;
        dp[sy][sx][0] = dp[sy][sx][1] = 0;
        mm.insert(make_pair(0, make_pair(make_pair(sy, sx), 0)));
        mm.insert(make_pair(0, make_pair(make_pair(sy, sx), 1)));

        while(!mm.empty()){
            int t = mm.begin()->first;
            int y = mm.begin()->second.first.first;
            int x = mm.begin()->second.first.second;
            int d = mm.begin()->second.second;
            mm.erase(mm.begin());
            if(t > dp[y][x][d])
                continue;

            if(s[y][x] == 0){
                int t2 = t % (ns[y][x] + ew[y][x]);
                if(d == 0){
                    if(t2 >= ns[y][x])
                        t += -t2 + (ns[y][x] + ew[y][x]);
                }else{
                    if(t2 < ns[y][x])
                        t += -t2 + ns[y][x];
                }
            }else{
                int t2 = t % (ew[y][x] + ns[y][x]);
                if(d == 0){
                    if(t2 < ew[y][x])
                        t += -t2 + ew[y][x];
                }else{
                    if(t2 >= ew[y][x])
                        t += -t2 + (ew[y][x] + ns[y][x]);
                }
            }

            if(y == gy && x == gx && t < INF){
                cout << t << endl;
                break;
            }

            for(int i=0; i<2; ++i){
                int t2 = t + dy[y-1+i];
                int y2 = y - 1 + 2*i;
                int x2 = x;
                int d2 = 0;
                if(t2 < dp[y2][x2][d2]){
                    dp[y2][x2][d2] = t2;
                    mm.insert(make_pair(t2, make_pair(make_pair(y2, x2), d2)));
                }
            }
            for(int i=0; i<2; ++i){
                int t2 = t + dx[x-1+i];
                int y2 = y;
                int x2 = x - 1 + 2*i;
                int d2 = 1;
                if(t2 < dp[y2][x2][d2]){
                    dp[y2][x2][d2] = t2;
                    mm.insert(make_pair(t2, make_pair(make_pair(y2, x2), d2)));
                }
            }
        }
    }
}