#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
inline int in(){
int x=0; char ch=getchar();
while (ch<'0' || ch>'9') ch=getchar();
while (ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
return x;
}
int main(){
freopen("cowroute.in","r",stdin);
freopen("cowroute.out","w",stdout);
int s=in(),t=in(),n=in(),ans=0x7fffffff;
for (int i=1; i<=n; i++){
int v=in(),num=in();
bool f1=false,f2=false;
for (int j=1; j<=num; j++){
int x=in();
if (s==x && !f2) f1=true;
if (t==x && f1) f2=true;
}
if (f1 && f2) ans=min(ans,v);
}
if (ans==0x7fffffff) ans=-1;
printf("%d\n",ans);
return 0;
}