The city of Siruseri is impeccably planned. The city is divided into a rectangular array of cells with $M$ rows and $N$ columns. Each cell has a metro station. There is one train running left to right and back along each row, and one running top to bottom and back along each column. Each trains starts at some time $T$ and goes back and forth along its route (a row or a column) forever.
Ordinary trains take two units of time to go from one station to the next. There are some fast trains that take only one unit of time to go from one station to the next. Finally, there are some slow trains that take three units of time to go from one station the next. You may assume that the halting time at any station is negligible.
Here is a description of a metro system with $3$ rows and $4$ columns:
$ $
S(1)  F(2)  O(2)  F(4)
F(3)    .     .     .     .
S(2)    .     .     .     .
O(2)    .     .     .     .

$ $
The label at the beginning of each row/column indicates the type of train (F for fast, O for ordinary, S for slow) and its starting time. Thus, the train that travels along row 1 is a fast train and it starts at time $3$. It starts at station ($1$, $1$) and moves right, visiting the stations along this row at times $3, 4, 5$ and $6$ respectively. It then returns back visiting the stations from right to left at times $6, 7, 8$ and $9$. It again moves right now visiting the stations at times $9, 10, 11$ and $12$, and so on. Similarly, the train along column $3$ is an ordinary train starting at time $2$. So, starting at the station ($3$,$1$), it visits the three stations on column $3$ at times $2, 4$ and $6$, returns back to the top of the column visiting them at times $6,8$ and $10$, and so on.
Given a starting station, the starting time and a destination station, your task is to determine the earliest time at which one can reach the destination using these trains.
For example suppose we start at station ($2$,$3$) at time $8$ and our aim is to reach the station ($1$,$1$). We may take the slow train of the second row at time $8$ and reach ($2$,$4$) at time $11$. It so happens that at time $11$, the fast train on column $4$ is at ($2$,$4$) travelling upwards, so we can take this fast train and reach ($1$,$4$) at time $12$. Once again we are lucky and at time $12$ the fast train on row $1$ is at ($1$,$4$), so we can take this fast train and reach ($1$, $1$) at time $15$. An alternative route would be to take the ordinary train on column $3$ from ($2$,$3$) at time $8$ and reach ($1$,$3$) at time $10$. We then wait there till time $13$ and take the fast train on row $1$ going left, reaching ($1$,$1$) at time $15$. You can verify that there is no way of reaching ($1$,$1$) earlier than that.

-----Input:-----
The first line contains two integers $M$ and $N$ indicating the number rows and columns in the metro system. This is followed by $M$ lines, lines $2, 3, …, M+1$, describing the trains along the $M$ rows. The first letter on each line is either F or O or S, indicating whether the train is a fast train, an ordinary train or a slow train. Following this, separated by a blank space, is an integer indicating the time at which this train starts running. The next $N$ lines, lines $M+2, M+3, …, N+M+1$, contain similar descriptions of the trains along the $N$ columns. The last line, line $N+M+2$, contains $5$ integers $a, b, c, d$ and $e$ where ($a$,$b$) is the starting station, $c$ is the starting time and ($d$,$e$) is the destination station.

-----Output:-----
A single integer indicating the earliest time at which one may reach the destination.

-----Constraints:-----
- $1 \leq M, N \leq 50$.
- $1 \leq a, d \leq M$
- $1 \leq b, e \leq N$
- $1 \leq$ all times in input $\leq 20$

-----Sample Input-----
3 4
F 3 
S 2
O 2
S 1
F 2
O 2
F 4
2 3 8 1 1 

-----Sample Output-----
15

```python

USER_FILENAME = "<user_code>"
import sys, json, builtins
import math, heapq, bisect, itertools, functools, collections, statistics, fractions, decimal, operator, array, random, re, string, os, pathlib
try:
    import resource as _res
except Exception:
    _res = None
from collections import Counter, defaultdict, deque, OrderedDict
from functools import lru_cache, reduce
from itertools import accumulate, permutations, combinations, product
from math import gcd, ceil, floor, sqrt
from decimal import Decimal
try:
    from typing import *
except Exception:
    pass
try:
    sys.setrecursionlimit(1_000_000)
except Exception:
    pass
try:
    import site as _site
    try:
        _sys_sites = _site.getsitepackages() if hasattr(_site, "getsitepackages") else []
        for _p in _sys_sites:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
    try:
        _usp = _site.getusersitepackages()
        if isinstance(_usp, str):
            _usp = [_usp]
        for _p in _usp:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
except Exception:
    pass

ns = {"__name__": "__main__"}
for _k, _v in list(globals().items()):
    if not _k.startswith("_"):
        ns[_k] = _v
_CODE = "from queue import PriorityQueue\r\nm,n=list(map(int,input().split()))\r\nrr=[]\r\ncc=[]\r\nspeed={'S':3,'O':2,'F':1}\r\nvisited=set()\r\ndp=[]\r\n\r\ndef qwerty(cur,x,y,f):\r\n\tif f==0:\r\n\t\tgg=rr[x][1]+y*rr[x][0]\r\n\t\twhile gg<cur:\r\n\t\t\tgg+=(2*(n-1))*rr[x][0]\r\n\t\treturn gg-cur+rr[x][0]\r\n\telif f==1:\r\n\t\tgg=rr[x][1]+(2*(n-1)-y)*rr[x][0]\r\n\t\twhile gg<cur:\r\n\t\t\tgg+=(2*(n-1))*rr[x][0]\r\n\t\treturn gg-cur+rr[x][0]\r\n\telif f==2:\r\n\t\tgg=cc[y][1]+x*cc[y][0]\r\n\t\twhile gg<cur:\r\n\t\t\tgg+=(2*(m-1))*cc[y][0]\r\n\t\treturn gg-cur+cc[y][0]\t\t\r\n\telif f==3:\r\n\t\tgg=cc[y][1]+(2*(m-1)-x)*cc[y][0]\r\n\t\twhile gg<cur:\r\n\t\t\tgg+=(2*(m-1))*cc[y][0]\r\n\t\treturn gg-cur+cc[y][0]\r\n\r\n\r\ndirx=[0, 0, 1, -1]\r\ndiry=[1, -1, 0, 0]\r\n\r\nfor i in range(m):\r\n\to=[x for x in input().split()]\r\n\to[0]=speed[o[0]]\r\n\to[1]=int(o[1])\r\n\trr.append(o)\r\n\r\n\r\nfor i in range(n):\r\n\to=[x for x in input().split()]\r\n\to[0]=speed[o[0]]\r\n\to[1]=int(o[1])\r\n\tcc.append(o)\r\n\r\n\r\nsx,sy,stt,dx,dy=list(map(int,input().split()))\r\nsx-=1\r\nsy-=1\r\ndx-=1\r\ndy-=1\r\n\r\nfor i in range(m):\r\n\tdp.append([10**9]*n)\r\n\r\ndp[sx][sy]=stt\r\npq = PriorityQueue()\r\npq.put((stt, sx, sy))\r\nwhile not pq.empty():\r\n\t#print(dp)\r\n\t(t,cxx,cyy)=pq.get()\r\n\tif (cxx,cyy) in visited:\r\n\t\tcontinue\r\n\tvisited.add((cxx,cyy))\r\n\tfor i in range(len(dirx)):\r\n\t\tnxx=cxx+dirx[i]\r\n\t\tnyy=cyy+diry[i]\r\n\t\tif nxx>=0 and nxx<m and nyy>=0 and nyy<n and (nxx,nyy) not in visited:\r\n\t\t\tcoo=qwerty(dp[cxx][cyy],cxx,cyy,i)\r\n\t\t\tif coo+dp[cxx][cyy]<dp[nxx][nyy]:\r\n\t\t\t\t\tdp[nxx][nyy]=coo+dp[cxx][cyy]\r\n\t\t\t\t\tpq.put((dp[nxx][nyy],nxx,nyy))\r\n\r\nprint(dp[dx][dy])\r\n\r\n\r\n\r\n\r\n\r\n\r\n\t\r\n\r\n\r\n\r\n"
_co = compile(_CODE, USER_FILENAME, "exec")
exec(_co, ns, ns)

```

Measured memory_bytes: 289502