/*
 * Remember a 2.0 student can know more than a 4.0 student.
 * Grades don't determine intelligence, they test obedience. 
 * I Never Give Up.
 */
import java.util.*;
import java.io.*;

import static java.lang.System.out;
//import static java.lang.Math.pow;
//import static java.lang.Math.sqrt;
//import static java.lang.Math.ceil;
//import static java.lang.Math.abs;
import static java.lang.Math.min;
import static java.lang.Math.max;
//import static java.lang.Math.floor;
public class ContestMain {
	private static long MOD=1000000007;//10^9+7
	private static final int N=(int) 2e5+5; //200005
	private static Scanner in=new Scanner(System.in);
	private static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//	private static ArrayList<Integer> v[]=new ArrayList[N];
//	private static boolean mark[]=new boolean[N];
//	static int choice=Integer.MAX_VALUE,trav=Integer.MAX_VALUE;
	
	public static void solve(long ar[],int n,int m,int k){
		
	}
	
	public static void main(String[] args) throws IOException{
		int n=in.nextInt();
		int m=in.nextInt();
		int ar[]=new int[n];
		int ar2[]=new int[m];
		for(int i=0;i<n;i++)
			ar[i]=in.nextInt();
		for(int i=0;i<m;i++)
			ar2[i]=in.nextInt();
		Arrays.sort(ar2);
		Arrays.sort(ar);
		int result=-1;
		for(int i=0;i<n;i++){
			if(Arrays.binarySearch(ar2, ar[i])>=0){
				result=ar[i];
				break;
			}
		}
		if(result==-1)out.println(min(ar[0],ar2[0])+""+max(ar[0],ar2[0]));
		else out.println(result);
	}
}