n,m=map(int,input().split())
list1=list(map(int,input().split()))[:n]
list2=list(map(int,input().split()))[:n]
list3=list(map(int,input().split()))[:n]
name=1#目前的出牌人
card_dict={'n':0,'type':0,'len':1}
def win():
if len(list1)==0 or len(list2)==0 or len(list3)==0:
return 1
def find_who(na):
if na==1:
find_list=list1
elif na==2:
find_list=list2
else:
find_list=list3
return find_list
def quiter():
i=0
if card_dict['n']==0:
pass
quit_list=find_who(card_dict['n'])
for i in range(card_dict['len']):
quit_list.remove(card_dict['type'])
def judge(na):#判断是否能打出
global name
judge_list=find_who(na)
flag=0
if na==card_dict['n']:
card_dict['type'] = 1
card_dict['len'] =0
for num in range(m+1):
if num in judge_list:
if num>card_dict['type'] and judge_list.count(num)>=card_dict['len']:
flag=1
break
if flag==1:#从等级上压制
card_dict['n'] = na
card_dict['type']=num
name+=1
if flag !=1:
for num in range(m+1):
if num in judge_list and judge_list.count(num)>card_dict['len']:
flag=2
break
if flag==2:#从数量上压制
card_dict['n']=na
card_dict['type']=num
card_dict['len']=card_dict['len']+1
name += 1
if flag==0:#失败了,跳过
name += 1
if flag!=0:
quiter()
flag = 0 # 重置
while True:
if win()==1:
print(card_dict['n'])
break
if name==4:
name=1
judge(name)