#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
typedef long long int ll;
struct numinfo
{
ll num;
ll pre;
};
ll gcd(ll a,ll b){
if(b==0)return a;
return gcd(b,a%b);
}
map< pair<ll,ll>, ll > mp;
ll cx,n,k1,k2,x,y,num2,num1,test,test2,top1,top2,dig2,dig1;
int main(int argc, char const *argv[])
{
cin.sync_with_stdio(0);
cin>>n;
cin>>k1;
num1=0;
cx=0;
test=test2=0;
for (int i = 0; i <k1; ++i)
{
cin>>x;
if(x==10)
{
test=1;
}
num1=num1*10+x%10;
}
cin>>k2;
num2=0;
for (int i = 0; i < k2; ++i)
{
cin>>x;
if(x==10)
{
test2=1;
}
num2=num2*10+x%10;
}
while(1)
{
// cout<<num1<<" "<<num2<<endl;
x=num1;
y=num2;
dig1=dig2=0;
if(mp[make_pair(num1,num2)]==1)
{
cout<<"-1"<<endl;
return 0;
}
mp[make_pair(num1,num2)]=1;
while(x>0)
{
top1=x;
x=x/10;
dig1++;
}
while(y>0)
{
top2=y;
y=y/10;
dig2++;
}
if (dig1+dig2==n && (num1==0 || num2==0 ) )
{
break;
}
if (dig1+dig2!=n && ( num1==0 || num2==0 ) )
{
if (test==1 && num2==0)
{
break;
}
if (test2==1 && num1==0)
{
break;
}
}
if(dig1+dig2!=n)
{
if(test==1)
{
ll mo;
num1=(((num1)*10+top2)*10);
mo=pow(10,dig2-1);
num2=num2%mo;
}
if(test2==1)
{
ll mo;
num2=(((num2)*10+top1)*10);
mo=pow(10,dig1-1);
num1=num1%mo;
}
}
else
{
if(top1>top2)
{
ll mo=pow(10,dig1-1);
num1=(((num1%mo)*10+top2)*10+top1);
mo=pow(10,dig2-1);
num2=num2%mo;
}
else
{
ll mo=pow(10,dig2-1);
num2=(((num2%mo)*10+top1)*10+top2);
mo=pow(10,dig1-1);
num1=num1%mo;
}
}
cx++;
}
cout<<cx<<" ";
if(num2==0)
{
cout<<"1"<<endl;
return 0;
}
if (num1==0)
{
cout<<"2"<<endl;
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgbW9kIDEwMDAwMDAwMDcKdHlwZWRlZiBsb25nIGxvbmcgaW50IGxsOwpzdHJ1Y3QgbnVtaW5mbwp7CiAgICBsbCBudW07CiAgICBsbCBwcmU7Cn07CmxsIGdjZChsbCBhLGxsIGIpewogICAgICBpZihiPT0wKXJldHVybiBhOwogICAgICByZXR1cm4gZ2NkKGIsYSViKTsKfQptYXA8IHBhaXI8bGwsbGw+LCBsbCA+IG1wOwoKbGwgY3gsbixrMSxrMix4LHksbnVtMixudW0xLHRlc3QsdGVzdDIsdG9wMSx0b3AyLGRpZzIsZGlnMTsKCmludCBtYWluKGludCBhcmdjLCBjaGFyIGNvbnN0ICphcmd2W10pCnsKICAgICAgICBjaW4uc3luY193aXRoX3N0ZGlvKDApOwoKICAgICAgICBjaW4+Pm47CiAgICAgICAgY2luPj5rMTsKICAgICAgICBudW0xPTA7CiAgICAgICAgY3g9MDsKICAgICAgICB0ZXN0PXRlc3QyPTA7CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPGsxOyArK2kpCiAgICAgICAgewogICAgICAgICAgICBjaW4+Png7CiAgICAgICAgICAgIGlmKHg9PTEwKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICB0ZXN0PTE7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgbnVtMT1udW0xKjEwK3glMTA7CiAgICAgICAgfQogICAgICAgIGNpbj4+azI7CiAgICAgICAgbnVtMj0wOwogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgazI7ICsraSkKICAgICAgICB7CiAgICAgICAgICAgIGNpbj4+eDsKICAgICAgICAgICAgaWYoeD09MTApCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHRlc3QyPTE7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgbnVtMj1udW0yKjEwK3glMTA7CiAgICAgICAgfQogICAgICAgIHdoaWxlKDEpCiAgICAgICAgewoKICAgICAgICAgIC8vIGNvdXQ8PG51bTE8PCIgIjw8bnVtMjw8ZW5kbDsKICAgICAgICAgICAgeD1udW0xOwogICAgICAgICAgICB5PW51bTI7CiAgICAgICAgICAgIGRpZzE9ZGlnMj0wOwogICAgICAgICAgICBpZihtcFttYWtlX3BhaXIobnVtMSxudW0yKV09PTEpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGNvdXQ8PCItMSI8PGVuZGw7CiAgICAgICAgICAgICAgICByZXR1cm4gMDsKICAgICAgICAgICAgfQogICAgICAgICAgICBtcFttYWtlX3BhaXIobnVtMSxudW0yKV09MTsKICAgICAgICAgICAgd2hpbGUoeD4wKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICB0b3AxPXg7CiAgICAgICAgICAgICAgICB4PXgvMTA7CiAgICAgICAgICAgICAgICBkaWcxKys7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgd2hpbGUoeT4wKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICB0b3AyPXk7CiAgICAgICAgICAgICAgICB5PXkvMTA7CiAgICAgICAgICAgICAgICBkaWcyKys7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKGRpZzErZGlnMj09biAmJiAobnVtMT09MCB8fCBudW0yPT0wICkgKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoZGlnMStkaWcyIT1uICYmICggbnVtMT09MCB8fCBudW0yPT0wICkgKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBpZiAodGVzdD09MSAmJiBudW0yPT0wKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYgKHRlc3QyPT0xICYmIG51bTE9PTApCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CgogICAgICAgICAgICB9CgogICAgICAgICAgICBpZihkaWcxK2RpZzIhPW4pCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGlmKHRlc3Q9PTEpCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgbGwgbW87CiAgICAgICAgICAgICAgICAgICAgbnVtMT0oKChudW0xKSoxMCt0b3AyKSoxMCk7CiAgICAgICAgICAgICAgICAgICAgbW89cG93KDEwLGRpZzItMSk7CiAgICAgICAgICAgICAgICAgICAgbnVtMj1udW0yJW1vOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgaWYodGVzdDI9PTEpCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgbGwgbW87CiAgICAgICAgICAgICAgICAgICAgbnVtMj0oKChudW0yKSoxMCt0b3AxKSoxMCk7CiAgICAgICAgICAgICAgICAgICAgbW89cG93KDEwLGRpZzEtMSk7CiAgICAgICAgICAgICAgICAgICAgbnVtMT1udW0xJW1vOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgaWYodG9wMT50b3AyKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIGxsIG1vPXBvdygxMCxkaWcxLTEpOwogICAgICAgICAgICAgICAgICAgIG51bTE9KCgobnVtMSVtbykqMTArdG9wMikqMTArdG9wMSk7CiAgICAgICAgICAgICAgICAgICAgbW89cG93KDEwLGRpZzItMSk7CiAgICAgICAgICAgICAgICAgICAgbnVtMj1udW0yJW1vOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIGxsIG1vPXBvdygxMCxkaWcyLTEpOwogICAgICAgICAgICAgICAgICAgIG51bTI9KCgobnVtMiVtbykqMTArdG9wMSkqMTArdG9wMik7CiAgICAgICAgICAgICAgICAgICAgbW89cG93KDEwLGRpZzEtMSk7CiAgICAgICAgICAgICAgICAgICAgbnVtMT1udW0xJW1vOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CgogICAgICAgICAgICBjeCsrOwogICAgICAgIH0KICAgICAgICBjb3V0PDxjeDw8IiAiOwogICAgICAgIGlmKG51bTI9PTApCiAgICAgICAgewogICAgICAgICAgICBjb3V0PDwiMSI8PGVuZGw7CiAgICAgICAgICAgIHJldHVybiAwOwogICAgICAgIH0KICAgICAgICBpZiAobnVtMT09MCkKICAgICAgICB7CiAgICAgICAgICAgIGNvdXQ8PCIyIjw8ZW5kbDsKICAgICAgICB9CiAgICAgICAgCiAgICAgICAgcmV0dXJuIDA7Cn0KICA=