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<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_W = 100;
const int MAX_H = 100;
const int MAX_HW = MAX_H * MAX_W;

const int INF = 1 << 30;

const int dxs[] = {  1,  0, -1,  0 };
const int dys[] = {  0,  1,  0, -1 };

/* typedef */

struct Signal {
  int nst, ewt, itv, s;
};

struct Stat {
  int d, pos, drc;
  Stat() {}
  Stat(int _d, int _pos, int _drc) { d = _d, pos = _pos, drc = _drc; }
  bool operator<(const Stat& st0) const { return d > st0.d; }
  void print() { printf("d=%d, pos=%d, drc=%d\n", d, pos, drc); }
};

enum { D_NS = 0, D_EW = 1 };

typedef vector<Stat> vs;

/* global variables */

int w, h, hw;
int dws[MAX_W], dhs[MAX_H];
Signal sgnls[MAX_HW + 2];

vs nbrs[MAX_HW + 2];
int dists[MAX_HW + 2][2];

/* subroutines */

void onroad(int x, int n, int *ds, int& i, int& dx) {
  i = 0, dx = x;
  while (i < n && dx >= ds[i]) dx -= ds[i], i++;
}

int time_go(Signal& sgnl, int t, int drc) {
  int rt = t % sgnl.itv;
  int tg = -1;
  
  if (sgnl.s == D_NS) {	// nst -> ewt
    if (drc == D_NS) tg = (rt < sgnl.nst) ? t : sgnl.itv * (t / sgnl.itv + 1);
    else tg = (rt >= sgnl.nst) ? t : (t / sgnl.itv) * sgnl.itv + sgnl.nst;
  }
  else {	// ewt -> nst
    if (drc == D_EW) tg = (rt < sgnl.ewt) ? t : sgnl.itv * (t / sgnl.itv + 1);
    else tg = (rt >= sgnl.ewt) ? t : (t / sgnl.itv) * sgnl.itv + sgnl.ewt;
  }

  return tg;
}

/* main */

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

    hw = h * w;
    
    for (int i = 0; i < w - 1; i++) cin >> dws[i];
    for (int i = 0; i < h - 1; i++) cin >> dhs[i];

    for (int y = 0; y < h; y++)
      for (int x = 0; x < w; x++) {
	int pos = y * w + x;
	Signal& sgnl = sgnls[pos];
	cin >> sgnl.nst >> sgnl.ewt >> sgnl.s;
	sgnl.itv = sgnl.nst + sgnl.ewt;

	nbrs[pos].clear();

	for (int di = 0; di < 4; di++) {
	  int x0 = x + dxs[di];
	  int y0 = y + dys[di];
	  if (x0 >= 0 && x0 < w && y0 >= 0 && y0 < h) {
	    int pos0 = y0 * w + x0;
	    int ec, edrc;
	    if (x0 != x) {
	      ec = dws[(x0 < x) ? x0 : x];
	      edrc = D_EW;
	    }
	    else {
	      ec = dhs[(y0 < y) ? y0 : y];
	      edrc = D_NS;
	    }
	    nbrs[pos].push_back(Stat(ec, pos0, edrc));
	  }
	}
      }

    int sx, sy;
    cin >> sx >> sy;

    int sxi, syi, sdx, sdy;
    onroad(sx, w - 1, dws, sxi, sdx);
    onroad(sy, h - 1, dhs, syi, sdy);
    //printf("(sxi,syi)=(%d,%d), (sdx,sdy)=(%d,%d)\n", sxi, syi, sdx, sdy);
    
    nbrs[hw].clear();
    
    if (sdx != 0) {
      int pos0 = syi * w + sxi, pos1 = pos0 + 1;
      nbrs[hw].push_back(Stat(sdx, pos0, D_EW));
      nbrs[hw].push_back(Stat(dws[sxi] - sdx, pos1, D_EW));
      sgnls[hw].nst = 0;
      sgnls[hw].ewt = 1;
      sgnls[hw].itv = 1;
      sgnls[hw].s = D_EW;
    }
    else {
      int pos0 = syi * w + sxi, pos1 = pos0 + w;
      nbrs[hw].push_back(Stat(sdy, pos0, D_NS));
      nbrs[hw].push_back(Stat(dhs[syi] - sdy, pos1, D_NS));
      sgnls[hw].nst = 1;
      sgnls[hw].ewt = 0;
      sgnls[hw].itv = 1;
      sgnls[hw].s = D_NS;
    }

    int gx, gy;
    cin >> gx >> gy;

    int gxi, gyi, gdx, gdy;
    onroad(gx, w - 1, dws, gxi, gdx);
    onroad(gy, h - 1, dhs, gyi, gdy);
    //printf("(gxi,gyi)=(%d,%d), (gdx,gdy)=(%d,%d)\n", gxi, gyi, gdx, gdy);

    if (gdx != 0) {
      int pos0 = gyi * w + gxi, pos1 = pos0 + 1;
      nbrs[pos0].push_back(Stat(gdx, hw + 1, D_EW));
      nbrs[pos1].push_back(Stat(dws[gxi] - gdx, hw + 1, D_EW));
    }
    else {
      int pos0 = gyi * w + gxi, pos1 = pos0 + w;
      nbrs[pos0].push_back(Stat(gdy, hw + 1, D_NS));
      nbrs[pos1].push_back(Stat(dhs[gyi] - gdy, hw + 1, D_NS));
    }

    if (sxi == gxi && syi == gyi) {
      if (sdy == 0 && gdy == 0) {
	int dx = (gdx > sdx) ? gdx - sdx : sdx - gdx;
	nbrs[hw].push_back(Stat(dx, hw + 1, D_EW));
      }
      else if (sdx == 0 && gdx == 0) {
	int dy = (gdy > sdy) ? gdy - sdy : sdy - gdy;
	nbrs[hw].push_back(Stat(dy, hw + 1, D_NS));
      }
    }
    
    for (int pos = 0; pos < hw + 2; pos++)
      for (int drc = 0; drc < 2; drc++)
	dists[pos][drc] = INF;

    dists[hw][0] = dists[hw][1] = 0;

    priority_queue<Stat> q;
    q.push(Stat(0, hw, sgnls[hw].s));

    int mind = INF;
    
    while (! q.empty()) {
      Stat u = q.top();
      q.pop();
      //u.print();
      
      if (u.d != dists[u.pos][u.drc]) continue;
      if (u.pos == hw + 1) {
	mind = u.d;
	break;
      }

      int ud = time_go(sgnls[u.pos], u.d, u.drc);
      vs& nbru = nbrs[u.pos];

      for (vs::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {
	Stat& v = *vit;
	int vd =  ud + v.d;

	if (dists[v.pos][v.drc] > vd) {
	  dists[v.pos][v.drc] = vd;
	  q.push(Stat(vd, v.pos, v.drc));
	}
      }
    }

    cout << mind << endl;
  }

  return 0;
}