#include <bits/stdc++.h>
using namespace std;
int ma[11][11][11][11],n,m,p;
vector<int> ys[11][11];
int vis[11][11][2050];
int b[8]={0,1,1,0,0,-1,-1,0};
int zt(int x,int y)
{
int res=1;
for(int i=0;i<y;i++) res<<=1;
if(x&res) return res;
return res+x;
}
struct edge{
int x,y,zt,len;
bool operator <(edge a) const{
return a.len<len;
}
};
bool check(int x,int y)
{
return x>=1&&y>=1&&x<=n&&y<=m;
}
void bfs()
{
priority_queue<edge> q;
for(int i=0;i<=10;i++)
{
for(int j=0;j<=10;j++)
{
for(int k=0;k<=2048;k++)
{
vis[i][j][k]=2e9;
}
}
}
vis[1][1][0]=0;
q.push(edge{1,1,0,0});
while(!q.empty())
{
edge ne=q.top();
int x=ne.x,y=ne.y;
cout << endl << x << ' ' << y << ' ' << ne.zt << ' ' << vis[x][y][ne.zt] << endl;
if(ys[x][y].size()!=0)
{
for(int i=0;i<ys[x][y].size();i++)
{
ne.zt=zt(ne.zt,ys[x][y][i]);
}
vis[x][y][ne.zt]=vis[x][y][q.top().zt];
}
cout << ne.zt << endl;
q.pop();
for(int i=0;i<4;i++)
{
int tx=x+b[i*2];
int ty=y+b[i*2+1];
if(!check(tx,ty)) continue;
cout << x << ' ' << y << ' ' << tx << ' ' << ty << ' ' << ma[x][y][tx][ty] << ' ' << ne.zt << endl;
if(ma[x][y][tx][ty]==1) continue;
if((!ma[x][y][tx][ty]||(zt(0,ma[x][y][tx][ty])&ne.zt))&&(vis[tx][ty][ne.zt]>ne.len+1))
{
vis[tx][ty][ne.zt]=ne.len+1;
q.push(edge{tx,ty,ne.zt,vis[tx][ty][ne.zt]});
}
}
}
}
int main()
{
cin >> n >> m >> p;
int x1,y1,x2,y2,S,f;
cin >> f;
while(f--)
{
cin >> x1 >> y1 >> x2 >> y2 >> S;
ma[x1][y1][x2][y2]=S+1;
}
cin >> f;
while(f--)
{
cin >> x1 >> y1 >> S;
ys[x1][y1].push_back(S+1);
}
bfs();
int ans=2e9;
for(int i=0;i<=2048;i++)
{
ans=min(vis[n][m][i],ans);
}
cout << ans;
return 0;
}