You are given two lists of non-zero digits.Let's call an integer pretty if its (base 10) representation has at least one digit from the first list and at least one digit from the second list. What is the smallest positive pretty integer?

The first line contains two integers n and m (1 ≤ n, m ≤ 9) — the lengths of the first and the second lists, respectively. The second line contains n distinct digits a1, a2, ..., an (1 ≤ ai ≤ 9) — the elements of the first list. The third line contains m distinct digits b1, b2, ..., bm (1 ≤ bi ≤ 9) — the elements of the second list.

NoteIn the first example 25, 46, 24567 are pretty, as well as many other integers. The smallest among them is 25. 42 and 24 are not pretty because they don't have digits from the second list.In the second example all integers that have at least one digit different from 9 are pretty. It's obvious that the smallest among them is 1, because it's the smallest positive integer.

program a1;
var k,i,m,n,x,min:integer;
    a: array [1..9] of integer;
    b: array [1..9] of integer;
Begin
 min:=99;
 readln(n,m);
 for i:=1 to n do read(a[i]);
 for i:=1 to m do read(b[i]);
 for i:=1 to n do
  begin
   for k:=1 to m do
   begin
    x:=(a[i]*10)+b[k];
    if (a[i]=b[k]) then x:=a[i];
    if x<min then min:=x;
   end;
  end;
 for i:=1 to m do
  begin
   for k:=1 to n do
   begin
    x:=(b[i]*10)+a[k];
    if (b[i]=a[k]) then x:=b[i];
    if x<min then min:=x;
   end;
  end;
  writeln(min);
End.

type mas=array[1..100] of integer;
var a,b,c:mas;
n,m,i,k1,k2,k:integer;
f:boolean;

function    min(var a:mas; k:longint):longint;
var i,l:longint;
begin

    l:=1;
    for i:=2 to k do
        if (a[i])<a[l]
            then
                l:=i;
    min:=a[l];

end;

function    zap(var a:mas; x,k:longint):boolean;
var i:Longint;
begin

    zap:=false;
    For i:=1 to k do
        If a[i]=x
            then
                zap:=true;

end;
begin

  readln(n,m);
  For i:=1 to n do
      read(a[i]);
  For i:=1 to m do
      read(b[i]);
  k1:=min(a,n);
  k2:=min(b,m);
  f:=true;
  k:=0;
  for i:=1 to n do
    If zap(b,a[i],m)
        then
            begin
                f:=false;
                k:=k+1;
                c[k]:=a[i];
            end;
    if f=false
        then
            write(min(c,k));
  if f=true
    then
  if (k1 < k2)
      then
          write(k1*10+k2)
      else
          if k1=k2
            then
              write(k1)
            else
              write(k2*10+k1);
end.

var i, i1, n1, n2, x, y, z : integer;
a: array[1..9] of integer;
b: array[1..9] of integer;
  begin
    read(n1, n2);

    for i:=1 to n1 do read(a[i]);

    for i:=1 to n2 do read(b[i]);

    x:=10;
    y:=10;

      for i:=1 to n1 do
      begin
      if a[i]<x then
      x:=a[i];
      end;

      for i:=1 to n2 do
      begin
      if b[i]<y then
      y:=b[i];
      end;

        if x<y then z:=x*10+y
        else if x>y then z:=y*10+x
        else z:=x;

                        for i:=1 to n1 do
                        begin

                         for i1:=1 to n2 do
                         begin

                           if a[i]=b[i1] then
                           begin

                            if a[i]<z then z:=a[i];

                           end;

                         end;

                       end;
       writeln(z);

  end.

var
	n, m, l, t, k: longint;
	a, b: array of integer;

begin

	readln(n, m);
	SetLength(a, n);
	SetLength(b, m);

	l := 10;
	t := 10;
	k := 10;

	for var i := 0 to n - 1 do
	begin
		read(a[i]);
		t := min(t, a[i]);
	end;

	for var i := 0 to m - 1 do
	begin
		read(b[i]);
		k := min(k, b[i]);
	end;

	for var i := 0 to n - 1 do
		for var j := 0 to m - 1 do
		begin
			if (a[i] = b[j]) then
				l := min(l, a[i]);
		end;

	if (l <> 10) then
		writeln(l)
	else begin
		if (k = t) then
			writeln(k)
		else
			writeln(min(k, t), max(k, t));
	end;

end.

program qwerrty;
var n,otv,f,m,j,i,d:longint;
l1,l2:boolean;
a,b:array [1..100] of longint;
begin
  readln(n,m);
  for i:=1 to n do read(a[i]);
  for i:=1 to m do read(b[i]);
  for i:=0 to 100 do begin
    f:=i;
    while f<>0 do begin
      d:=f mod 10;
      for j:=1 to n do if d=a[j] then l1:=true;
      for j:=1 to m do if d=b[j] then l2:=true;
      f:=f div 10;
    end;
    if (otv=0) and l1 and l2 then otv:=i;
    l1:=false;
    l2:=false;
  end;
  writeln(otv);
end.

var s,j,min2,min,n,m,i:integer;
a:array[1..10] of integer;
b:array[1..10]  of integer;
begin
read(n);
read(m);
for i:=1 to n do
  read(a[i]);
for i:=1 to m do
  read(b[i]);
s:=10;
for i:=1 to n do
for j:=1 to m do
    if (a[i]=b[j]) and (a[i]<s)  then s:=a[i];

if s<>10 then writeln(s)
  else
    begin
      min:=a[1];
      for i:=2 to n do
        if min>a[i] then min:=a[i];
        min2:=b[1];
      for j:=2 to m do
        if min2>b[j] then min2:=b[j];

if min=min2 then    writeln(min2)
else
if min<min2 then writeln(min,min2)
  else writeln(min2,min);
  end;
end.

var
m,n,i,j,max,max2,x:integer;
a:array[1..10] of integer;
b:array[1..10] of integer;
begin
max:=10;
max2:=10;
x:=10;
readln (m,n);
for i:=1 to m do
  begin
  read (a[i]);
  end;
for j:=1 to n do
  begin
  read (b[j]);
  end;

for i:=1 to m do
  begin
  for j:=1 to n do
    begin
    if (a[i]=b[j]) and (a[i]<x) then
      begin
      x:=a[i];
      end;
    end;
  end;

if x=10 then
  begin
  for i:=1 to m do
    begin
    if a[i]<max then max:=a[i];
    end;
  for j:=1 to n do
    if b[j]<max2 then max2:=b[j];

  if max>max2 then writeln (max2,max)
  else writeln (max,max2);
  end
  else writeln (x);

end.

var s,n,m,i,j:integer;
    a,b:array of integer;
begin
read(n,m);
SetLength(a,n);
SetLength(b,m);
for i:=0 to n-1 do read(a[i]);
for j:=0 to m-1 do read(b[j]);
s:=a[0]*10 + b[0];
for i:= 0 to n-1 do
begin
for j:= 0 to m-1 do
begin
if (a[i]*10+b[j] < s) and (b[j]*10 +a[i] > a[i]*10 + b[j])then s:=a[i]*10+b[j]
else if (b[j]*10 +a[i] < a[i]*10 + b[j]) and (b[j]*10 +a[i]<s)then s:=b[j]*10 + a[i];
if (a[i]=b[j]) and (a[i]<s) then s:=a[i];
end;
end;
write(s);
end.

Var n,m,i,k,d,s,f:integer;
a:array[1..9] of integer;
b:array[1..9] of integer;
Begin
Readln(n,m);
d:=10;
f:=10;
s:=10;
for i:=1 to n do begin
read(a[i]);
if a[i]<f then f:=a[i];
end;
For k:=1 to m do begin
read(b[k]);
if b[k]<s then s:=b[k];
end;
for i:=1 to n do
 for k:=1 to m do
 if (b[k]=a[i]) and (b[k]<d) then d:=b[k];
 if d<>10 then writeln(d) else if s>f then writeln(f,s) else writeln(s,f);
 end.

Program z1;
var m,n,i,cr,min,min2,k:integer;a:array[1..10] of integer;b:array[1..10] of integer;
begin
readln(n,m);
for i:=1 to n do begin
read(a[i]);
if i=1 then min:=a[i];
if a[i]<min then min:=a[i];
end;
for i:=1 to m do begin
read(b[i]);
if i=1 then min2:=b[i];
if b[i]<min2 then min2:=b[i];
end;
for i:=1 to n do begin
for k:=1 to m do begin
if a[i]=b[k] then begin
if (cr=0)or (cr>a[i]) then cr:=a[i];
end;end;end;
if cr>0 then writeln(cr) else begin if min>min2 then writeln(min+min2*10)else writeln(min*10+min2);end;
end.

Program bbb;
Var s1,s2:set of 1..9;
    a,min1,min2,n,m,i:byte;
Begin
  Read(n,m);
  For i:=1 to n do
    Begin
      Read(a);
      If i=1 then min1:=a;
      If a<min1 then min1:=a;
      s1:=s1+[a];
    End;
For i:=1 to m do
    Begin
      Read(a);
      If i=1 then min2:=a;
      If a<min1 then min2:=a;
      s2:=s2+[a];
    End;
  n:=0;
 For i:=1 to 9 do If (i in s1)and(i in s2)and(n=0) then Begin n:=1; Writeln(i); End;
 If n=0 then If min1<min2 then Writeln(min1*10+min2) else Writeln(min2*10+min1);
End.

var n,m,i,min1,min2:longint;
    a,b,f:array[0..10] of longint;
begin
  readln(n,m);
  min1:=10;min2:=10;
  for i:=1 to n do
  begin
    read(a[i]);
	if a[i]<min1 then min1:=a[i];
  end;
  for i:=1 to m do
  begin
    read(b[i]);
	if b[i]<min2 then min2:=b[i];
  end;
  for i:=1 to n do inc(f[a[i]]);
  for i:=1 to m do inc(f[b[i]]);
  for i:=1 to 9 do
    if f[i]>1 then begin writeln(i); halt; end;
  if min1<min2 then writeln(min1,min2) else writeln(min2,min1);
end.

var i,j,n,m,min1,min2,count : integer;
arr1: array [1..10] of integer;
arr2: array [1..10] of integer;
begin
min1:=10;
min2:=10;
read(n);
read (m);
readln;
for i:=1 to n do
begin
read(arr1[i]);
end;

for i:=1 to m do
begin
read(arr2[i]);
end;

for i:=1 to n do
 for j:=1 to m do
 if (arr1[i]=arr2[j]) and (arr1[i]<min1) then
 begin
 min1:=arr1[i];
 count:=count+1;
 end;
if count>0 then
begin
write(min1);
exit;
end;

for i:=1 to m do
if (arr2[i]<min2) and (arr2[i]>0) then
min2:=arr2[i];
for i:=1 to m do
if (arr1[i]<min1) and (arr1[i]>0) then
min1:=arr1[i];

if min1>min2 then
write (min2,min1);
if min1<min2 then
write (min1,min2);
end.

VAR n,m,i,j,min1,min2,s:integer;
p:boolean;
a:array[1..9] of integer;
b:array[1..9] of integer;
BEGIN
p:=false;
readln(n,m);

read(a[1]);
min1:=a[1];

for i:=2 to n do begin
read(a[i]);
if a[i]<=min1 then min1:=a[i];
end;

read(b[1]);
min2:=b[1];

for i:=2 to m do begin
read(b[i]);
if b[i]<=min2 then min2:=b[i];
end;

for i:=1 to n do
for j:=1 to m do
if a[i]=b[j] then begin p:=true; s:=a[i]; break; end;

for i:=1 to n do
for j:=1 to m do
if (a[i]=b[j]) and (a[i]<s) then s:=a[i];

IF p=true then write(s) else
if min1=min2 then write(min1)
else if min1<min2 then write(min1*10+min2) else write(min2*10+min1);
END.

var
  n, m, i, j, min1, min2, j1, same: integer;
  a, b: array [1..9] of integer;

begin
  same := 10;
  min1 := 10;
  min2 := 10;
  read(n, m);

  for i := 1 to n do
  begin
    read(a[i]);
    if a[i] < min1 then min1 := a[i];
  end;

  for j := 1 to m do
  begin
    read(b[j]);
    if b[j] < min2 then min2 := b[j];
    for j1 := 1 to n do
      if a[j1] = b[j] then
      begin
         if b[j] < same then same:= b[j];

      end;
  end;

  if same <> 10 then writeln(same)
  else if min1 = min2 then writeln (min1)
  else if min1 < min2 then writeln(min1, min2)
  else if min1 > min2 then writeln(min2, min1)

end.

var n, m, min1, min2, i, k, j: byte;
    a, b: array [1..9] of byte;

begin
     Read (n, m);
     min1 := 10;
     min2 := 10;
     k := 0;

     for i := 1 to n do
         Read (a [i]);

     for i := 1 to m do
         Read (b [i]);

     for i := 1 to n do
         if a [i] < min1 then min1 := a [i];

     for i := 1 to m do
         if b [i] < min2 then min2 := b [i];

     if min1 = min2 then k := min1
                    else if min1 > min2 then k := min2 * 10 + min1
                                        else k := min1 * 10 + min2;
     for i := 1 to n do
         for j := 1 to m do
             if (a [i] = b [j]) and (a [i] < k) then k := a [i];

     WriteLn (k);
end.

var a,b:array[0..9]of boolean;
    n,m,x,y,i,j:longint;
begin
  readln(n,m);
  for i:=1 to n do
  begin
    read(x);
    a[x]:=true;
  end;
  for i:=1 to m do
  begin
    read(x);
    b[x]:=true;
  end;
  for i:=1 to 9 do
    if a[i] and b[i] then
    begin
      writeln(i);
      halt;
    end;
  for i:=1 to 9 do
    if a[i] then
    begin
      x:=i;
      break;
    end;
  for i:=1 to 9 do
    if b[i] then
    begin
      if x>i then writeln(i,x)
             else writeln(x,i);
      halt;
    end;
end.

program Kor;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  sizeA,minA,minB,i,sizeB,k,zor:Integer;
  A,B:Array[1..9]of Integer;
begin

minA:=11;
zor:=11;
minB:=11;
readln(sizeA,sizeB);
for i:=1 to sizeA do begin
  read(A[i]);
  if A[i]<minA then
    minA:=A[i];
  end;
for i:=1 to sizeB do begin
  read(B[i]);
  if B[i]<minB then
    minB:=B[i];
  end;
zor:=11;
for i:=1 to sizeA do  begin
  for k:=1 to sizeB do begin
    if (A[i]=B[k])and(A[i]<zor)then
      zor:=A[i];
  end;
end;
if zor=11 then begin
  if (minA>minB) then
    write(minA+minB*10)
  else
    write(minB+minA*10);
  end
else
  write(zor);

  { TODO -oUser -cConsole Main : Insert code here }
end.

program jdfh;
var a, b, i, j, t:integer; n:array [1..9] of integer;
begin
read(a, b);
for i:=1 to a do begin
    read(t);
    n[t]:=n[t]+1;
end;
for i:=1 to b do begin
    read(t);
    n[t]:=n[t]+2;
end;
for i:=1 to 9 do
    if n[i]=3 then begin
        write(i);
        exit;
    end;
i:=1;
while n[i]<1 do i:=i+1;
write(i);
j:=1;
while (n[j]=n[i]) or (n[j]=0) do j:=j+1;
write(j);
end.

program cras;
var n,m,x,i: integer;
    a,b: array [1..9] of integer;
begin
  for i:=1 to 9 do
    begin
      a[i]:=0;
      b[i]:=0;
    end;
  read(n,m);
  for i:=1 to n do
    begin
      read(x);
      a[x]:=1;
    end;
  for i:=1 to m do
    begin
      read(x);
      b[x]:=1;
    end;
  i:=1;
  while(a[i]=0) do i:=i+1;
  x:=1;
  while(b[x]=0) do x:=x+1;
  if x>i then x:=i*10+x
  else if i>x then x:=x*10+i;
  for i:=1 to 9 do
    if (a[i]=1) and (b[i]=1) then
      if x>i then
        begin
          x:=i;
          break;
        end;
  writeln(x);
end.

                    const eps=0.0000000001;  besk=20000000000008;  nn=200000;
   label 1,2,3,4;
    type
    mas=array [0..nn] of int64;   point=record x,y,z:real;end;
    zap=record p1,p2,p3 :int64;end; zap1=^zap;
    mass=array[0..nn] of int64;mass1=^mass;
    var pp,qq,aaa,bbb,ccc,tt,aa,bb,jj,kk,ii,xx,yy,ott,time,ppp,istart,jstart,iii,newdl, t1,t2,cc,ll,rr,oldpos,t,jjj,kol,pos,dloch,told,p,q,pmin,ppold,ot,pmax,imax,p1,p2,p3,p4,l,nm,n,s2,m,smin,cen:int64;
   mask,k,i2,i3,i1,j1,k1, step, i,j:longint;
    s0,s1,aanew,bbnew:extended;
    sa,sb,sia,sib,s,d11,a,b,right,aold,left,z,x,newc,d,c,nap:array[-10..1000001] of int64;
    y:array[-10..10000000] of extended;
    d1,ddd,dd:array[-10..100,0..100,0..100] of int64;
    ddminus, ppole:array[-1..2000,-100..1502] of int64;
   sss,text1,text2:array[-10..2000000] of string;
     stest,ss3,ss1,ss2:string;
     ch1,ch2:char;
     ddd1:array[-1..400000,-1..10] of int64;
     marked:array[-100..500,-100..500] of boolean;
    {sss:string;ch:char;
   } pole :array [-10..500,-10..500] of char;
    st:array[0..nn] of string;
    stest1,ss:string;
     v0,v1,t0:real;
     zv:point;
     newx,newy:array[-1..200000] of int64;

     koef,yot:extended;
      globflag, flag2,flag,flag1:boolean;
    function al(x,y:real):real; var t0,t1,p,s0,s1:real;
 begin
     if (x>0)and(y>=0) then p:=arctan(y/x);
     if x<0 then p:=pi+arctan(y/x);
     if(x>0)and(y<0) then p:=2*pi+arctan(y/x);
     if x=0 then if (y>=0) then p:=pi/2 else p:=3*pi/2;
     al:=p/pi*180;
 end;
  function min(x,y:int64):int64;begin min:=x;if y<x then min:=y; end;
  function max(x,y:int64):int64;begin max:=x;if y>x then max:=y; end;

  function nod(x,y:int64):int64; var z:int64;
begin   repeat  z:=x mod y;x:=y;y:=z;   until  z=0 ;  nod:=x;end;

begin
  {  assign (input,'F:\Projects\input.txt');reset(input);
      assign (output,'F:\Projects\output.txt'); rewrite(output);
   }

     readln(n,m);
     for i:=1 to n do read(a[i]);
     for i:=1 to m do read(b[i]);

     t:=10000;
     for i:=1 to n do for j:=1 to m do if a[i]=b[j] then
       begin  t:=min(t,a[i]); flag:=true;  end;

        if flag then begin  writeln(t); goto 1;  end;

   t1:=1000;
   t2:=1000;
   for i:=1 to n do t1:=min(t1,a[i]);

   for i:=1 to m do t2:=min(t2,b[i]);

     if t1<t2 then write(t1,t2)  else write(t2,t1);

   1:    close(output);
end.

VAR
a,b:array[1..9]of byte;
One,Two,i,j,n,m:byte;
BEGIN
readln(n,m);
for i:=1 to n do read(a[i]);
readln;
for i:=1 to m do read(b[i]);
for i:=1 to 9 do
 begin
  One:=0; Two:=0;
  for j:=1 to n do
    if a[j]=i then One:=1;
  for j:=1 to m do
    if b[j]=i then Two:=1;
  if ((One=1) and (Two=1)) then begin writeln(i); halt; end;
end;
One:=10; Two:=10;
for j:=1 to n do
  if a[j]<One then One:=a[j];
for j:=1 to m do
  if b[j]<Two then Two:=b[j];
if One>Two then begin i:=One; One:=Two; Two:=i; end;
writeln(One,Two);
END.

var n,m,a,i,min1,min2:integer;
c:array[1..9] of integer;
begin
    readln(n,m);
    min1:=10;
    min2:=10;
    for i:=1 to n do
    begin
       read(a);
       c[a]:=c[a]+1;
       if a < min1 then min1:=a;
    end;
    n:=10;
    for i:=1 to m do
    begin
       read(a);
       if (c[a] <> 0) and (a < n) then n:=a;
       if a < min2 then min2:=a;
    end;
    if n <> 10 then write(n)
    else if min1 < min2 then write(min1,min2)
    else if min1 > min2 then write(min2,min1);
end.

uses    math;
const   fi='';
var     a:array[1..10] of longint;
        b:array[1..10] of longint;
        d:array[1..10] of boolean;
        n,m,a1,a2,i,j:longint;f:text;
procedure swap(var x,y:longint);
var     w:longint;
begin
        w:=x;x:=y;y:=w;
end;
begin
        assign(f,fi);reset(f);
        readln(f,n,m);a1:=10;a2:=10;
        for i:=1 to n do read(f,a[i]);
        for i:=1 to m do read(f,b[i]);close(f);
        for i:=1 to n do a1:=min(a[i],a1);
        for i:=1 to m do a2:=min(b[i],a2);
        for i:=1 to m-1 do
                for j:=i+1 to m do if b[i]>b[j] then swap(b[i],b[j]);
        for i:=1 to n do d[a[i]]:=true;
        for i:=1 to m do if d[b[i]] then begin write(b[i]);halt;end;
        if a1=a2 then write(a1) else if a1>a2 then write(a2,a1)
        else write(a1,a2);readln
end.

program chisla;

var
  n, m, min1,min2,mind,j,i: integer;
  a: array[1..9] of integer;
  b: array[1..9] of integer;

begin
  Read(n, m);
  for i := 1 to n do
    Read(a[i]);
    for i := 1 to m do
   Read(b[i]);
  min1:=10;
  min2:=10;
  mind:=10;
  for i:=1 to n do
  if a[i]<min1 then
  min1:=a[i];
   for i:=1 to m do
  if b[i]<min2 then
  min2:=b[i];
  for i:=1 to n do
  for j:=1 to m do
  if (a[i]=b[j])and(a[i]<mind) then
  mind:=a[i];
  if mind<10 then
  Write(mind)
  else if min1<min2 then
  Write(min1*10+min2)
  else
  Write(min2*10+min1);
end.

Program primer;
label 1;
var m,n,mina,minb,res,j,k,x,y:Integer;
i:longint;
fl1,fl2 : boolean;
a: array[0..10] of Integer;
b: array[0..10] of Integer;

Begin
Readln (n,m);
for i:=1 to n do
Read(a[i]);
for i:=1 to m do
Read(b[i]);
for i:=0 to 10000 do
begin

fl1:=false;
fl2:=false;
x:=i;
  repeat
    y := x mod 10;
for j:=1 to n do
if (y = a[j]) then fl1 := true;
for k:=1 to m do
if (y = b[k]) then fl2 := true;
    x := x div 10;
  until x = 0;
if fl1 and fl2 then goto 1;

end;
1:
WriteLn(i);
End.

var
  n,n1,i,k,k1,i1,kr,nr:integer;
  x,x1,xr:array of integer;
  b:boolean;
begin
  read(n,n1);

  SetLength(x,n);
  SetLength(x1,n1);
  for i:=0 to n-1 do
  begin
    read(x[i]);
  end;
  for i:=0 to n1-1 do
  begin
    read(x1[i]);
  end;
  k:=x[0];
  k1:=x1[0];

  for i:= 1 to n-1 do
  begin
    if x[i]<k then k:=x[i];
  end;

  for i:= 1 to n1-1 do
  begin
    if x1[i]<k1 then k1:=x1[i];
  end;

  for i:=0 to n-1 do
  begin
    for i1:= 0 to n1-1 do
    begin
      if x[i]=x1[i1] then
      begin
        nr:=nr+1;
        SetLength(xr,nr);
        xr[nr-1]:=x[i];
        b:=true;
      end;
    end;
  end;

  if b then
  begin
  kr:=xr[0];

  for i:= 1 to nr-1 do
  begin
    if xr[i]<kr then kr:=xr[i];
  end;
  end;

  if not b then
  begin
    if k>k1 then write(k1*10+k) else write(k*10+k1);
  end;

  if b then write (kr);

end.

var
  n, m, i,  j, min1, min2, k, l: integer;
  a: array [1..9] of integer;
  b: array [1..9] of integer;

begin
  read(n, m);
  L := 0;
  if (n <= 9) and (n >= 1) and (m >= 1) and (m <= 9) then
  begin
    min1 := 10;
    for i := 1 to n do
    begin
      read(a[i]);
      if (a[i] > 9) or (a[i] < 1) then
      begin
        l := 1;
        break;
      end;
      if a[i] < min1 then
        min1 := a[i];
    end;
    if l = 0 then
    begin
      min2 := 10;
      for i := 1 to m do
      begin
        read(b[i]);
        if (b[i] > 9) or (b[i] < 1) then
        begin
          l := 1;
          break;
        end;
        if b[i] < min2 then
          min2 := b[i];
      end;
      if l = 0 then
      begin
        k := 10;
        for i := 1 to n do
          for J := 1 to m do
            if (a[i] = b[j]) and (k > a[i]) then
              k := a[i];
        if (k < 10) then
          writeln(k)
        else
        if min1<min2 then
          writeln(min1, min2)
          else
          writeln(min2, min1)
      end;
    end;
  end;
end.

type mas=array[1..1000] of integer;

procedure Min(X: mas; kol:integer; var Xmin: integer);
var i: integer;
begin
Xmin:=X[1];
for i:=2 to kol do if X[i]<Xmin then Xmin:=X[i];
end;

var n,m,i,j,k,Amin,Bmin,Cmin: integer; A,B,C: mas;
begin
readln(n,m); Amin:=0; Bmin:=0; Cmin:=0;
for i:=1 to n do read (A[i]);
for i:=1 to m do read (B[i]);
k:=0;
for i:=1 to n do
  for j:=1 to m do if A[i]=B[j] then begin
                                     k:=k+1;
                                     C[k]:=A[i];
                                     end;
if k=0 then BEGIN
Min(A,n,Amin);
Min(B,m,Bmin);
if Amin>=Bmin then writeln(Bmin,Amin)
              else writeln(Amin,Bmin);
           END
       else begin
            Min(C,k,Cmin);
            writeln (Cmin);
            end;
end.

program p1;
var i,n,m,min,max,x,y,k,j,q,o:integer;
a,b:array[0..11] of integer;
begin
  readln(n,m);
  for i:=1 to n do read(a[i]);
  for i:=1 to m do read(b[i]);
  q:=a[1];
  x:=a[1];
  y:=b[1];
  for i:=2 to n do if a[i]<x then x:=a[i];
  for i:=2 to m do if b[i]<y then y:=b[i];
  for i:=2 to n do if a[i]>q then q:=a[i];
  //for i:=1 to n do writeln(a[i]);
  //writeln(x, ' ',y);
  for i:=1 to n do
    for j:=1 to m do if a[i]=b[j] then if o=0 then begin
      k:=a[i];
      o:=1;
    end else if k>a[i] then k:=a[i];
  if x>y then begin
    min:=y;
    max:=x
  end else begin
    min:=x;
    max:=y;
  end;
  if o<>0 then writeln(k) else if min=max then writeln(min) else writeln(min,max);
end.

var
m,n,i,j,max,max2,x:integer;
a:array[1..10] of integer;
b:array[1..10] of integer;
begin
max:=10;
max2:=10;
x:=10;
readln (m,n);
for i:=1 to m do
  begin
  read (a[i]);
  end;
for j:=1 to n do
  begin
  read (b[j]);
  end;

for i:=1 to m do
  begin
  for j:=1 to n do
    begin
    if (a[i]=b[j]) and (a[i]<x) then
      begin
      x:=a[i];
      end;
    end;
  end;

if x=10 then
  begin
  for i:=1 to m do
    begin
    if a[i]<max then max:=a[i];
    end;
  for j:=1 to n do
    if b[j]<max2 then max2:=b[j];

  if max>max2 then writeln (max2,max)
  else writeln (max,max2);
  end
  else writeln (x);

end.

Var a, b, i, k, l, c, d: integer;
m, n: array [1..9] of integer;
Begin
read (a, b);
k:=10;
l:=10;
d:=10;
for i:= 1 to a do
begin
 read (n[i]);
 if n[i]<k then
 k:=n[i];
end;
for i:= 1 to b do
begin
 read (m[i]);
 if m[i]<l then
 l:=m[i];
end;
for i:= 1 to a do
for c:= 1 to b do
 if (n[i]=m[c]) and (n[i]<d) then
 d:=n[i];
 if d=10 then
 begin
 if k<l then
 writeln (k*10+l)
 else if k>l then
 writeln (l*10+k);
 end
 else writeln (d)
end.

var n,m,i,j,minA,minB,min:longint;
    a:array [1..9] of byte;
    b:array [1..9] of byte;
begin
  read(n,m);
  for i:=1 to n do read(a[i]);
  for i:=1 to m do read(b[i]);

  min:=10;
  for i:=1 to 9 do for j:=1 to 9 do if  (a[i]=b[j]) and (a[i]<min)
  and ((a[i]<>0) and (b[j]<>0))
                                    then min:=a[i];
             if min<>10 then write(min)
                      else begin

  minA:=10;
    for i:=1 to n do
    if a[i]<minA then minA:=a[i];
  minB:=10;
    for i:=1 to m do
    if b[i]<minB then minB:=b[i];

                if minA<minB then write(minA,minB)
                                   else write(minB,minA);
                        end;
end.

var n,m,i,j,outp,b:integer;
a:array[1..9] of integer;
begin
outp:=999;
readln(n,m);
for i:=1 to n do
read(a[i]);
for i:=1 to m do
begin
read(b);
for j:=1 to n do
if b=a[j] then
begin
if b<outp then
outp:=b;
end
else
begin
if (b*10+a[j])<outp then outp:=b*10+a[j];
if (b+a[j]*10)<outp then outp:=b+a[j]*10;
end;
end;
write(outp);
end.

var a,b:array[1..9] of longint;
i,j,m,n,k:longint;
function min(a,b:longint):longint;
begin
min:=a;
if a>b then min:=b;
end;
BEGIN
read(m,n);
k:=100;
for i:=1 to m do
read(a[i]);
for j:=1 to n do
read(b[j]);
for i:=1 to m do
for j:=1 to n do
begin
k:=min(k,a[i]*10+b[j]);
k:=min(k,b[j]*10+a[i]);
if a[i]=b[j] then k:=min(k,a[i]);
end;
write(k)
end.

var
  n,m,z,s,i,j:integer;
  a,b:array[1..9] of integer;
begin
  readln(n,m);
  for i:=1 to n do
    read(a[i]);
  for j:=1 to m do
    read(b[j]);
  for i:=2 to n do begin
  z:=i;
  while a[z]>a[z-1] do begin
   s:=a[z];
   a[z]:=a[z-1];
   a[z-1]:=s;
   dec(z);
   if z=1 then break;
  end;
  end;
  for j:=2 to m do begin
  z:=j;
  while b[z]>b[z-1] do begin
   s:=b[z];
   b[z]:=b[z-1];
   b[z-1]:=s;
   dec(z);
   if z=1 then break;
  end;
  end;
  z:=10;
  for i:=1 to n do
    for j:=1 to m do
      if (a[i]=b[j]) and (z>a[i]) then z:=a[i];
  if z<10 then writeln(z) else
    if a[n]>b[m] then writeln(b[m],a[n]) else
      if a[n]<b[m] then writeln(a[n],b[m]);
end.

var i,n,m,g,j,c1,c2:integer;
a,b: array[1..10] of integer;
begin
read(n,m);
g:=10;
c1:=10;
c2:=10;
for  i := 1 to n do
  begin
    read(a[i]);
    if(a[i]<c1)then c1:=a[i];
  end;
for  i := 1 to m do
  begin
    read(b[i]);
    for j := 1 to n do if(b[i]=a[j])and(a[j]<g)then g:=a[j]
                       else if(b[i]<c2)then c2:=b[i];
  end;
if(g<>10)then writeln(g)
else if (c1>c2)then writeln(c2,c1)
     else writeln(c1,c2);
end.

var a,b,i,c,min,j:shortint;
    MLP,FIM: array [1..10] of shortint;
begin
  read(a,b);
  for i:=1 to a do read(MLP[i]);
  for i:=1 to b do read(FIM[i]);
  for i:=1 to a-1 do
    for j:=1 to a-i do
      if (MLP[j+1]<MLP[j]) then
        begin
        c:=MLP[j+1];
        MLP[j+1]:=MLP[j];
        MLP[j]:=c;
        end;
  for i:=1 to b-1 do
    for j:=1 to b-i do
      if (FIM[j+1]<FIM[j]) then
        begin
        c:=FIM[j+1];
        FIM[j+1]:=FIM[j];
        FIM[j]:=c;
        end;
  c:=99;
  for i:=1 to a do
    for j:=1 to b do
      if (MLP[i]=FIM[j]) and (c=99) then
        begin
        c:=98;
        write(MLP[i]);
        end;
  if (c=99) then
    begin
    for i:=1 to a do
      if (MLP[i]<c) then c:=MLP[i];
    a:=c;
    c:=99;
    for i:=1 to b do
      if (FIM[i]<c) then c:=FIM[i];
    if (c<a) then write(c,a)
    else write(a,c)
    end;
end.

var i, i1, n1, n2, x, y, z : integer;
a: array[1..9] of integer;
b: array[1..9] of integer;
  begin
    read(n1, n2);

    for i:=1 to n1 do read(a[i]);

    for i:=1 to n2 do read(b[i]);

    x:=10;
    y:=10;

      for i:=1 to n1 do
      begin
      if a[i]<x then
      x:=a[i];
      end;

      for i:=1 to n2 do
      begin
      if b[i]<y then
      y:=b[i];
      end;

        if x<y then z:=x*10+y
        else if x>y then z:=y*10+x
        else z:=x;

                        for i:=1 to n1 do
                        begin

                         for i1:=1 to n2 do
                         begin

                           if a[i]=b[i1] then
                           begin

                            if a[i]<z then z:=a[i];

                           end;

                         end;

                       end;
       writeln(z);

  end.

var n,m,a,i,j,min1,min2,min: byte;
arr1, arr2 : array[1..10] of byte;
begin
readln(n, m);
min1:=10;
min2:=10;
min:=10;
for i:=1 to n do
  read(arr1[i]);

for i:=1 to m do
  read(arr2[i]);

if n<m then begin
             for i:=1 to n do
               for j:=1 to m do
                  if (arr1[i] = arr2[j]) and (min > arr2[j]) then min:=arr1[i];
            end;

if n>m then begin
             for i:=1 to m do
               for j:=1 to n do
                  if (arr2[i] = arr1[j]) and (min > arr1[j]) then min:=arr2[i];
            end;

if n=m then begin
             for i:=1 to m do
               for j:=1 to n do
                  if (arr2[i] = arr1[j]) and (min > arr1[j]) then min:=arr2[i];
            end;

if min=10 then begin
                  for i:=1 to m do
                    if arr2[i] < min2 then min2:=arr2[i];
                  for i:=1 to n do
                    if arr1[i] < min1 then min1:=arr1[i];
                  if min2 > min1 then write(min1,min2) else write(min2,min1);
                 end
                 else
                 write(min);
end.

program ff;

var
  n, m, am, bm, i, mm, j: integer;
  a, b: array[1..10] of integer;

begin

  readln(n, m);

  am := 100;
  bm := 100;
  mm := 100;

  for i := 1 to n do
  begin
    read(a[i]);
    if a[i] < am then am := a[i];
  end;

  for i := 1 to m do
  begin
    read(b[i]);
    if b[i] < bm then bm := b[i];
  end;

  for i:=1 to n do
    for j:=1 to m do
      if (a[i]=b[j])and(a[i]<mm) then mm:=a[i];

  if mm <> 100 then write(mm) else if am < bm then writeln(am, bm) else writeln(bm, am);
end.

program hm;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  n, m, min1, j, i, c: integer;
  a, b: array[1..19] of integer;
begin
  readln(n, m);
  for i := 1 to n do
    read(a[i]);
  for i := 1 to m do
    read(b[i]);

  for i := 2 to n do
    for j := n downto i do
    if (a[j] < a[j - 1]) then begin
      c := a[j];
      a[j] := a[j - 1];
      a[j - 1] := c;
    end;

  for i := 2 to m do
    for j := m downto i do
    if (b[j] < b[j - 1]) then begin
      c := b[j];
      b[j] := b[j - 1];
      b[j - 1] := c;
    end;

  for i := 1 to n do
    for j := 1 to m do
    if (a[i] = b[j]) then begin
      write(a[i]);
      exit;
    end;
  if (a[1] > b[1]) then write(b[1],a[1])
    else
  write(a[1], b[1]);

end.

program Project1;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes, SysUtils, CustApp
  { you can add units after this };
var
  min1, min2, min3, n, m, i, j: integer;
  a, b: array [1..1000000] of integer;
begin
  min1:=10;
  min2:=10;
  min3:=10;
  read (n, m);
  for i:=1 to n do
  begin
    read (a[i]);
    if (a[i]<min1) then min1:=a[i];
  end;
  for i:=1 to m do
  begin
    read (b[i]);
    if (b[i]<min2) then min2:=b[i];
  end;
  for i:=1 to n do
  begin
    for j:=1 to m do
    begin
      if (a[i]=b[j]) then
      begin
        if (a[i]<min3) then min3:=a[i];
      end;
    end;
  end;
  if (min3<>10) then writeln (min3)
  else
  begin
    if (min1>min2) then writeln (min2, min1)
    else writeln (min1, min2);
  end;
  readln;
  readln;
end.

var n, m, ma, mb, mo, i, j : integer;
    a, b : array [1..9] of integer;

begin

     readln(n, m);

     ma := 9;
     mb := 9;
     mo := 10;

     for i := 1 to n do
     begin
         read(a[i]);
         if a[i] < ma then ma := a[i];
     end;

     for i := 1 to m do
     begin
         read(b[i]);
         if b[i] < mb then mb := b[i];
     end;
     for i := 1 to n do
         for j := 1 to m do
             if (a[i] = b[j]) and (a[i] < mo) then mo := a[i];

     if mo < 10 then writeln(mo)
                else if ma > mb then writeln(mb*10 + ma)
                                else writeln(ma*10 + mb)
end.

program qwerrty;
var n,otv,f,m,j,i,d:longint;
l1,l2:boolean;
a,b:array [1..100] of longint;
begin
  readln(n,m);
  for i:=1 to n do read(a[i]);
  for i:=1 to m do read(b[i]);
  for i:=0 to 100 do begin
    f:=i;
    while f<>0 do begin
      d:=f mod 10;
      for j:=1 to n do if d=a[j] then l1:=true;
      for j:=1 to m do if d=b[j] then l2:=true;
      f:=f div 10;
    end;
    if (otv=0) and l1 and l2 then otv:=i;
    l1:=false;
    l2:=false;
  end;
  writeln(otv);
end.

Program bbb;
Var s1,s2:set of 1..9;
    a,min1,min2,n,m,i:byte;
Begin
  Read(n,m);
  For i:=1 to n do
    Begin
      Read(a);
      If i=1 then min1:=a;
      If a<min1 then min1:=a;
      s1:=s1+[a];
    End;
For i:=1 to m do
    Begin
      Read(a);
      If i=1 then min2:=a;
      If a<min1 then min2:=a;
      s2:=s2+[a];
    End;
  n:=0;
 For i:=1 to 9 do If (i in s1)and(i in s2)and(n=0) then Begin n:=1; Writeln(i); End;
 If n=0 then If min1<min2 then Writeln(min1*10+min2) else Writeln(min2*10+min1);
End.

Program P1;
var n,m,i,a,b,i2,e,e2: integer; a1,a2: array [0..1000] of integer;
begin
a:=10; b:=10; e:=10;
readln(n,m);
for i:= 1 to n do
read(a1[i]);

for i:= 1 to m do
read(a2[i]);

for i:= 1 to n do begin
if a1[i]<a then a:=a1[i];

for i2:= 1 to m do
if (a2[i2]=a1[i]) and (a2[i2]<e) then e:=a2[i2];
end;
if e=10 then begin
for i:= 1 to m do
if a2[i]<b then b:=a2[i];
end;

if e<10 then
write(e) else

if a>=b then
write(b,a)

else
write(a,b)
end.

var a,b:array[0..11] of longint;
    c,d:array[0..11] of boolean;
    i,j,n,m,x,y,k,ans:longint;
function min(a,b:longint):longint;
 begin
  if a<b then exit(a) else exit(b);
 end;
procedure init;
 begin
  readln(n,m);
  for i:=1 to n do
   read(a[i]);
  for i:=1 to m do
   read(b[i]);
 end;
procedure main;
 begin
  for i:=1 to n do
   c[a[i]]:=true;
  for i:=1 to m do
   d[b[i]]:=true;
  for i:=1 to 9 do
    if (c[i])and(d[i]) then begin writeln(i); halt; end;
  for i:=1 to 9 do
   for j:=1 to 9 do
    if (c[i])and(d[j]) then begin writeln(min(i,j),i+j-min(i,j)); halt; end;
 end;
begin
 init;
 main;
end.

var a,b:array[0..9]of boolean;
    n,m,x,y,i,j:longint;
begin
  readln(n,m);
  for i:=1 to n do
  begin
    read(x);
    a[x]:=true;
  end;
  for i:=1 to m do
  begin
    read(x);
    b[x]:=true;
  end;
  for i:=1 to 9 do
    if a[i] and b[i] then
    begin
      writeln(i);
      halt;
    end;
  for i:=1 to 9 do
    if a[i] then
    begin
      x:=i;
      break;
    end;
  for i:=1 to 9 do
    if b[i] then
    begin
      if x>i then writeln(i,x)
             else writeln(x,i);
      halt;
    end;
end.

program n1;
var q,n,m,i,u,amin,bmin,cmin,t:integer;
    a,b,c: array [1..9] of integer;
begin;
read (n,m);
readln;
for i:=1 to n do
read (a[i]);
readln;
for i:=1 to m do
read (b[i]);

q:=1;

for i:=1 to n do
 for u:=1 to m do
  if (a[i]=b[u]) then
   begin
   c[q]:=a[i];
   q:=q+1;
   end;

 if (q=2) then
 t:=c[1]
 else
 begin
cmin:=c[1];
for i:=1 to q-1 do
if (cmin>c[i]) then cmin:=c[i];

if (cmin>0) then
t:=cmin
  else
  begin
amin:=a[1];
for i:=1 to n do
if (amin>a[i]) then amin:=a[i];
bmin:=b[1];
for i:=1 to m do
if (bmin>b[i]) then bmin:=b[i];
if (amin=bmin) then
t:=amin;
if (amin>bmin) then
t:=bmin*10+amin;
if (amin<bmin) then
t:=bmin+amin*10;
end;
end;
writeln (t);
end.

Uses math;
Const
        fin='';
        fon='';
        maxC=1000000000;
        maxN=1000+10;
	module=1000000007;

Type
	re=record
		x,y:longint;
		end;

Var
        fi,fo:text;
	n,mi,mii,m:longint;
        a,b:array[0..maxN] of longint;

Procedure mo;
Begin
        Assign(fi,fin); Reset(fi);
        Assign(fo,fon); Rewrite(fo);
End;

Procedure dong;
Begin
        Close(fi);
        Close(fo);
End;

Procedure nhap;
Var
	i:longint;
Begin
        mii:=10;
        mi:=10;
        Readln(fi,n,m);
        For i:=1 to n do
        Read(fi,a[i]);
        For i:=1 to m do
        Read(fi,b[i]);
End;

Function co(x,y:longint):boolean;
Var
        s,s1:ansistring;
        i,j:longint;
Begin
        Str(x,s);
        Str(y,s1);
        For i:=1 to length(s) do
        For j:=1 to length(s1) do
        If s[i]=s1[j] then exit(true);
        Exit(false);
End;

Procedure xuly;
Var
        i,j:longint;
        ok:boolean;
Begin
        For i:=1 to 99 do
        Begin
                ok:=false;
                For j:=1 to n do
                If co(i,a[j]) then
                Begin
                        ok:=true;
                        break;
                End;
                If not ok then continue;
                For j:=1 to m do
                If co(i,b[j]) then
                Begin
                        Writeln(fo,i);
                        Exit;
                End;
        End;
End;

Procedure xuat;
Begin
End;

BEGIN
        Mo;
        Nhap;
        Xuly;
        Xuat;
        Dong;
END.

program jdfh;
var a, b, i, j, t:integer; n:array [1..9] of integer;
begin
read(a, b);
for i:=1 to a do begin
    read(t);
    n[t]:=n[t]+1;
end;
for i:=1 to b do begin
    read(t);
    n[t]:=n[t]+2;
end;
for i:=1 to 9 do
    if n[i]=3 then begin
        write(i);
        exit;
    end;
i:=1;
while n[i]<1 do i:=i+1;
write(i);
j:=1;
while (n[j]=n[i]) or (n[j]=0) do j:=j+1;
write(j);
end.

program z1;
var a,b:array[1..9]of integer;i,m,n,p,k,h,j:integer;
procedure QuickSort( L, R : Integer ); { Быстрая сортировка массива A[] }
var i,j,x,y,m: integer;
begin
  i := l; j := r;
  x := a[(l+r) div 2];
  repeat
    while (A[i] < x) do inc(i);
    while (x < A[j]) do dec(j);
    if ( i <= j ) then
    begin
      y:=A[i]; a[i]:=a[j]; a[j]:=y;
            inc(i); dec(j);
    end;
  until (i > j);
  if (l < j) then QuickSort(l,j);
  if (i < r) then QuickSort(i,r);
end;
procedure QuickSort1( L, R : Integer ); { Быстрая сортировка массива A[] }
var i,j,x,y,m: integer;
begin
  i := l; j := r;
  x := b[(l+r) div 2];
  repeat
    while (b[i] < x) do inc(i);
    while (x < b[j]) do dec(j);
    if ( i <= j ) then
    begin
      y:=b[i]; b[i]:=b[j]; b[j]:=y;
            inc(i); dec(j);
    end;
  until (i > j);
  if (l < j) then QuickSort1(l,j);
  if (i < r) then QuickSort1(i,r);
end;
function min(k,h:integer):integer;
begin
if k>h then min:=h
else min:=k;end;
function max(n,m:integer):integer;
begin
if n<m then max:=m
else max:=n;end;
begin
read(n,m);
for i:=1 to n do
read(a[i]);quicksort(1,n);
for i:=1 to m do read(b[i]);
quicksort1(1,m);
k:=a[1]*10+b[1];
h:=b[1]*10+a[1];
p:=min(k,h);
for j:=1 to n do
for i:=1 to m do
if a[j]=b[i] then if a[j]<p then p:=a[j];
write(p);
end.

program Project2;

{$APPTYPE CONSOLE}
Var
a:array of byte;
an, bn, i, mina, minb, b, ab, j:byte;

begin
read(an, bn);
setlength(a, an);
mina:=10;
minb:=10;
ab:=10;
for i:=0 to an-1 do
  begin
  read(a[i]);
  if a[i]< mina then
    mina:=a[i];
  end;
for i:=1 to bn do
  begin
  read(b);
  for j:=0 to an-1 do
    if (a[j]=b) and (b<ab) then
      ab:=b;
  if b < minb then
    minb:=b;
  end;
if ab<>10 then
  write(ab) else
    if mina<minb then
      write(mina*10+minb) else
        write(minb*10+mina);
readln;
readln;
end.

Var
  n, m, min1, min2, min3: Int64;
  c1: Array [1..9] of Int64;
  c2: Array [1..9] of Int64;

Begin
  min1 := 10000;
  min2 := 10000;
  min3 := 10000;
  Read(n, m);
  For Var i := 1 to n do Begin
    Read(c1[i]);
    If c1[i] < min1 then
      min1 := c1[i];
  End;
  For Var i := 1 to m do Begin
    Read(c2[i]);
    If c2[i] < min2 then
      min2 := c2[i];
  End;
  For Var i := 1 to n do
    For Var j := 1 to m do
      If (c1[i] = c2[j]) and (c1[i] < min3) then
        min3 := c1[i];
  If min3 <> 10000 then
    Write(min3)
  Else
    Write(min(min1, min2),max(min1, min2))
End.

var
  i, j, n, m, k : integer;
  a, b : array [1..9] of integer;
begin
  read(n, m);
  for i := 1 to n do
    begin
      read(k);
      a[k] := 1;
    end;
  for i := 1 to m do
    begin
      read(k);
      b[k] := 1;
    end;
  for i := 1 to 9 do
    if (a[i] = 1) and (b[i] = 1)
      then
        begin
          writeln(i);
          exit;
        end;
  for i := 1 to 9 do
    for j := 1 to 9 do
      if (a[i] = 1) and (b[j] = 1)
        then
          begin
            if i > j
              then
                writeln(j, i)
              else
                writeln(i, j);
                exit;
          end;
end.

var i,n,m,g,j,c1,c2:integer;
a,b: array[1..10] of integer;
begin
read(n,m);
g:=10;
c1:=10;
c2:=10;
for  i := 1 to n do
  begin
    read(a[i]);
    if(a[i]<c1)then c1:=a[i];
  end;
for  i := 1 to m do
  begin
    read(b[i]);
    for j := 1 to n do if(b[i]=a[j])and(a[j]<g)then g:=a[j]
                       else if(b[i]<c2)then c2:=b[i];
  end;
if(g<>10)then writeln(g)
else if (c1>c2)then writeln(c2,c1)
     else writeln(c1,c2);
end.

program sha_sha_cama_pulya;

{$APPTYPE CONSOLE}

uses
  SysUtils, math;

var
  mid, n, m, x, i, j: integer;
  a, b: array[0..9] of integer;

begin
  readln(n, m);
  mid := maxint;
  for i := 1 to n do begin
    read(x);
    a[x] := 1;
  end;
  for i := 1 to m do begin
    read(x);
    if (a[x] = 1) then
      mid := min(mid, x);
    b[x] := 1;
  end;
  i := 0;
  while (a[i] <> 1) do
    inc(i);
  j := 0;
  while (b[j] <> 1) do
    inc(j);
  if (mid < maxint) then begin
    writeln(mid);
    exit;
  end;
  if (i < j) then
    write(i, j)
  else
    write(j, i);
  readln;
  readln;
end.

var
m,n,i,j,min1,min2,s:integer;
a,b:array [1..9] of integer;
begin
readln(n,m);
for i:=1 to n do
read(a[i]);
for i:=1 to m do
read(b[i]);

s:=10;
for i:=1 to n do begin
for j:=1 to m do
if (a[i]=b[j]) and (a[i]<s) then s:=a[i]
end;
if s<>10 then writeln(s) else
begin
min1:=a[1];
for i:=2 to n do
if min1>a[i] then min1:=a[i];
min2:=b[1];
for i:=2 to m do
if min2>b[i] then min2:=b[i];

if min1=min2 then writeln(min1) else
if min1<min2 then writeln(min1,min2) else writeln(min2,min1);
end
end.

var
  s, s1, s2: string;
  min1, min2, min, i, min11, n, m, x, code: integer;

begin
  readln(s);
  val(copy(s, 1, 1), n, code);
  val(copy(s, 3, 1), m, code);
  readln(s1);
  readln(s2);
  min1 := 10;
  min2 := 10;
  min11 := 10;
  i:=1;
  while i <= 2*n-1 do
  begin
    val(copy(s1, i, 1), x, code);
    if pos(copy(s1, i, 1), s2) <> 0 then
    begin
      if min11 > x then
        min11 := x;
    end
    else
    if min1 > x then
      min1 := x;
      i:=i+2;
  end;
  if min11 = 10 then
  begin
  i:=1;
    while i <= 2*m-1 do
    begin
      val(copy(s2, i, 1), x, code);
      if min2 > x then
        min2 := x;
        i:=i+2;
    end;
    if min1 > min2 then
      min := min2 * 10 + min1
    else
      min := min1 * 10 + min2;

  end
  else min := min11;
  writeln(min);
end.

program nomerA;
var n,m,i,k,min,g:integer;
mass1:array [1..9] of integer;
mass2:array [1..9] of integer;
mass3:array [1..10000] of integer;
begin
read(n,m);
for i:=1 to n do
read(mass1[i]);
for i:=1 to m do
read(mass2[i]);
for k:=1 to n do begin
for i:=1 to m do begin
g:=g+1;
if mass1[k] = mass2[i]
    then mass3[g]:=mass2[i]
    else
    mass3[g]:=10*mass1[k]+mass2[i];
end;
end;
for k:=1 to n do begin
for i:=1 to m do begin
g:=g+1;
if mass1[k] = mass2[i]
    then mass3[g]:=mass2[i]
    else
    mass3[g]:=mass1[k]+10*mass2[i];
end;
end;
k:=g;
min:=mass3[1];
for i:=2 to k do
if min > mass3[i] then min:=mass3[i];
writeln(min);
end.

var
   n,m,i,j,z,min1,min2,itog1,itog2,min3 : longint;
   n1,m1 : Array of integer;

begin
      readln(n,m);
      SetLength(n1,n);
      SetLength(m1,m);
      for i := 0 to n-1 do
      begin
            read(n1[i]);
      end;
      for i := 0 to m-1 do
      begin
            read(m1[i]);
      end;
      z := 0;
      min3 := 10;
      for i := 0 to n-1 do
      begin
            for j := 0 to m-1 do
            begin
                 if n1[i] = m1[j] then
                 begin
                      if n1[i] < min3 then
                        min3 := n1[i];
                      inc(z);
                 end;
            end;
      end;
      if z <> 0 then
      begin
            writeln(min3);
            exit;
      end;
      min1 := 10;
      min2 := 10;
      for i := 0 to n-1 do
      begin
            if n1[i] < min1 then
              min1 := n1[i]
      end;
      for j := 0 to m-1 do
      begin
            if m1[j] < min2 then
              min2 := m1[j]
      end;
      itog1 := (min1 * 10) + min2;
      itog2 := (min2 * 10) + min1;

      if itog1 < itog2 then
         writeln(itog1)
      else
         writeln(itog2);
end.

program hrn;
 var
  a,b,fa,fb:array[1..9]of byte;
  n,m,i,f,x,y:byte;
 begin
  read(n,m);
  for i:=1 to n do begin
   read(a[i]);fa[a[i]]:=1;end;
  for i:=1 to m do begin
   read(b[i]);fb[b[i]]:=1;end;
  for i:=1 to 9 do
   if(fa[i]=1)and(fb[i]=1)then begin
    write(i);f:=1;break;end;
  if f=0 then begin
   x:=10;y:=10;
   for i:=1 to n do if a[i]<x then x:=a[i];
   for i:=1 to m do if b[i]<y then y:=b[i];
   if x>y then begin
    x:=x+y;
    y:=x-y;
    x:=x-y;
   end;
   write(x,y);
  end;
 end.

var n,m,i,min1,min2:longint;
    a,b,f:array[0..10] of longint;
begin
  readln(n,m);
  min1:=10;min2:=10;
  for i:=1 to n do
  begin
    read(a[i]);
	if a[i]<min1 then min1:=a[i];
  end;
  for i:=1 to m do
  begin
    read(b[i]);
	if b[i]<min2 then min2:=b[i];
  end;
  for i:=1 to n do inc(f[a[i]]);
  for i:=1 to m do inc(f[b[i]]);
  for i:=1 to 9 do
    if f[i]>1 then begin writeln(i); halt; end;
  if min1<min2 then writeln(min1,min2) else writeln(min2,min1);
end.

var n,m,i,j,minA,minB,min:longint;
    a:array [1..9] of byte;
    b:array [1..9] of byte;
begin
  read(n,m);
  for i:=1 to n do read(a[i]);
  for i:=1 to m do read(b[i]);

  min:=10;
  for i:=1 to 9 do for j:=1 to 9 do if  (a[i]=b[j]) and (a[i]<min)
  and ((a[i]<>0) and (b[j]<>0))
                                    then min:=a[i];
             if min<>10 then write(min)
                      else begin

  minA:=10;
    for i:=1 to n do
    if a[i]<minA then minA:=a[i];
  minB:=10;
    for i:=1 to m do
    if b[i]<minB then minB:=b[i];

                if minA<minB then write(minA,minB)
                                   else write(minB,minA);
                        end;
end.

var a,b,c,d,e,f,g: integer;
m: array [1..10] of integer;
n: array [1..10] of integer;
begin
d:=100;
f:=10;
g:=10;
read(a);
readln(b);
for c:=1 to a do begin
read(m[c]);
if c=a then
end;
for c:=1 to b do begin
read(n[c]);
if c=b then
end;
for c:=1 to a do
for e:=1 to b do
if m[c]=n[e] then
if m[c]<d then
d:=m[c];
if d>10 then begin
for c:=1 to a do
if (m[c]<f) and (m[c]>0) then
f:=m[c];
for c:=1 to b do
if (n[c]<g) and (n[c]>0) then
g:=n[c];
if g>f then
d:=f*10+g
else
d:=g*10+f;
end;
write(d);
end.

var
  a, b: array [0..100] of integer;
  i, j, n, m, q, w, x: integer;

begin
  read(n, m);
  readln;
  for i := 0 to n - 1 do
  begin
    read(a[i]);
  end;
  readln;
  for j := 0 to m - 1 do
  begin
    read(b[j]);
  end;
  x:=3333;
  for i := 0 to n - 1 do begin
    for j := 0 to m - 1 do begin
      if(a[i]=b[j]) and (b[j]<x) then begin
      x:= b[j];
      end;
    end;
  end;
  q := 333;
  w := 333;
  for i := 0 to n - 1 do
  begin
    if(a[i] < q) then q := a[i];
  end;
  for j := 0 to m - 1 do
  begin
    if(b[j] < w) then w := b[j];
  end;

  if(q = w) then
    write(q)

  else if(x<10*q+w)and(x<10*w+q)then write(x)

  else if(q < w) then
    write(10*q+w)

  else if(q > w) then
    write(10*w+q);

end.

var a,b: array[1..9] of integer;
n,m,amin,bmin,ab,i,j:integer;
q:boolean;
begin
q:=false;
amin:=9;
bmin:=9;
ab:=9;
readln(n,m);
for i:=1 to n do begin
read(a[i]);
end;
for i:=1 to m do begin
read(b[i]);
end;
for i:=1 to n do begin
for j:=1 to m do begin
if (a[i]=b[j]) and (a[i]<=ab) then begin
ab:=a[i];
q:=true;
end;
end;
end;
amin:=a[1];
bmin:=b[1];
for i:=1 to n do begin
if a[i]<=amin then
amin:=a[i];
end;

for i:=1 to m do begin
if b[i]<=bmin then
bmin:=b[i];
end;

if not q then begin
if amin=bmin then begin
writeln(amin);
end
else
begin
if amin>bmin then begin
writeln(10*bmin+amin);
end
else
begin
writeln(10*amin+bmin);
end;
end;
end
else
begin
writeln(ab);
end;
end.

Program Tech1;
var  a,b,i,j,pov,m1,m2,tl:longint;
     m,p,v:array[0..10]of longint;

begin
read(a,b);
m1:=10;
m2:=10;
for i:=1 to a do
  begin
  read(p[i]);
  if p[i]<m1 then m1:=p[i];
  end;
for i:=1 to b do
  begin
  read(v[i]);
  if v[i]<m2 then m2:=v[i];
  end;
pov:=10;
for i:=1 to a do
  for j:=1 to b do
    if p[i]=v[j] then
      begin
      if p[i]<pov then pov:=p[i];
      end;
if pov<10 then writeln(pov) else
  begin
  if m1>m2 then
    begin
    tl:=m1;
    m1:=m2;
    m2:=tl;
    end;
  write(m1,m2);
  end;

end.

program ne3;
var i, n,k,ob, m,mina,minb:integer;
a,b:array [1..9] of integer;
prov:boolean;
begin
read(n,m);
mina:=9;
minb:=9;
ob:=9;
prov:=false;
for i:=1 to n do
begin
read(a[i]);
if a[i]<mina then mina:=a[i];
end;
for i:=1 to m do
begin
read(b[i]);
if b[i]<minb then minb:=b[i];
end;

for i:=1 to n do
 for k:=1 to m do
 if a[i]=b[k] then
 if a[i]<=ob then
 begin ob:=a[i]; prov:=true; end;

if prov then writeln(ob)
else
if mina=minb then writeln(mina)
else
if mina>minb then writeln(minb*10+mina)
else
if mina<minb then writeln(mina*10+minb);
end.

Var
  n, m, min1, min2, min3: Int64;
  c1: Array [1..9] of Int64;
  c2: Array [1..9] of Int64;

Begin
  min1 := 10000;
  min2 := 10000;
  min3 := 10000;
  Read(n, m);
  For Var i := 1 to n do Begin
    Read(c1[i]);
    If c1[i] < min1 then
      min1 := c1[i];
  End;
  For Var i := 1 to m do Begin
    Read(c2[i]);
    If c2[i] < min2 then
      min2 := c2[i];
  End;
  For Var i := 1 to n do
    For Var j := 1 to m do
      If (c1[i] = c2[j]) and (c1[i] < min3) then
        min3 := c1[i];
  If min3 <> 10000 then
    Write(min3)
  Else
    Write(min(min1, min2),max(min1, min2))
End.

program chisla;

var
  n, m, min1,min2,mind,j,i: integer;
  a: array[1..9] of integer;
  b: array[1..9] of integer;

begin
  Read(n, m);
  for i := 1 to n do
    Read(a[i]);
    for i := 1 to m do
   Read(b[i]);
  min1:=10;
  min2:=10;
  mind:=10;
  for i:=1 to n do
  if a[i]<min1 then
  min1:=a[i];
   for i:=1 to m do
  if b[i]<min2 then
  min2:=b[i];
  for i:=1 to n do
  for j:=1 to m do
  if (a[i]=b[j])and(a[i]<mind) then
  mind:=a[i];
  if mind<10 then
  Write(mind)
  else if min1<min2 then
  Write(min1*10+min2)
  else
  Write(min2*10+min1);
end.

Program P1;
var n,m,i,a,b,i2,e,e2: integer; a1,a2: array [0..1000] of integer;
begin
a:=10; b:=10; e:=10;
readln(n,m);
for i:= 1 to n do
read(a1[i]);

for i:= 1 to m do
read(a2[i]);

for i:= 1 to n do begin
if a1[i]<a then a:=a1[i];

for i2:= 1 to m do
if (a2[i2]=a1[i]) and (a2[i2]<e) then e:=a2[i2];
end;
if e=10 then begin
for i:= 1 to m do
if a2[i]<b then b:=a2[i];
end;

if e<10 then
write(e) else

if a>=b then
write(b,a)

else
write(a,b)
end.

program qq;
var n,m,i,c,amin,bmin,min:byte;
a,b: array [1..10] of integer;
begin
min:=10;
amin:=10;
bmin:=10;
readln (n,m);
for i:=1 to n do begin
read (a[i]);
if a[i]<amin then amin:=a[i];
end;
for i:=1 to m do begin
read (b[i]);
if b[i]<bmin then bmin:=b[i];
end;
for i:=1 to n do
for c:=1 to m do
if (a[i]=b[c]) and (a[i]<min) then min:=a[i];
if min<10 then write (min)
else if amin<bmin then write(10*amin+bmin)
else write (10*bmin+amin);
end.

uses math;
var a,b,tot:array[1..9] of longint;
    n,m,i,j,t:longint;
begin
  readln(n,m);
  for i:=1 to n do begin read(a[i]);inc(tot[a[i]]);end;
  for i:=1 to m do begin read(b[i]);inc(tot[b[i]]);end;
  for i:=1 to 9 do
    if tot[i]>1 then
    begin
      writeln(i);
      exit;
    end;
  for i:=1 to n-1 do
    for j:=i+1 to n do
      if a[i]>a[j] then
      begin
        t:=a[i];a[i]:=a[j];a[j]:=t;
      end;
  for i:=1 to m-1 do
    for j:=i+1 to m do
      if b[i]>b[j] then
      begin
        t:=b[i];b[i]:=b[j];b[j]:=t;
      end;
  writeln(min(a[1],b[1]),max(a[1],b[1]));
end.

var n,m,a,i,min1,min2:integer;
c:array[1..9] of integer;
begin
    readln(n,m);
    min1:=10;
    min2:=10;
    for i:=1 to n do
    begin
       read(a);
       c[a]:=c[a]+1;
       if a < min1 then min1:=a;
    end;
    n:=10;
    for i:=1 to m do
    begin
       read(a);
       if (c[a] <> 0) and (a < n) then n:=a;
       if a < min2 then min2:=a;
    end;
    if n <> 10 then write(n)
    else if min1 < min2 then write(min1,min2)
    else if min1 > min2 then write(min2,min1);
end.

program  fds;
var  n,m,i,r,t,x,q,k,j:integer;
     a:array[1..100]  of  integer;
     b:array[1..100]  of  integer;
     c:array[1..100]  of  integer;
begin
  r:=10;
  t:=10;
  q:=10;
  k:=0;
  read(n,m);
  for  i:=1  to  n  do  read(a[i]);
  for  i:=1  to  m  do  read(b[i]);
  for  i:=1  to  n  do  if  a[i]<r  then  r:=a[i];
  for  i:=1  to  m  do  if  b[i]<t  then  t:=b[i];
  for  i:=1  to  n  do
    for  j:=1  to  m  do  if  a[i]=b[j]  then
                                           begin
                                             k:=k+1;
                                             c[k]:=a[i];
                                           end;
  for  i:=1  to  k  do  if  c[i]<q  then  q:=c[i];
  if  q<10  then  writeln(q)
            else  if  t<r  then  writeln(t,r)
                           else  if  r<t  then  writeln(r,t);
end.

var
	n, m, l, t, k: longint;
	a, b: array of integer;

begin

	readln(n, m);
	SetLength(a, n);
	SetLength(b, m);

	l := 10;
	t := 10;
	k := 10;

	for var i := 0 to n - 1 do
	begin
		read(a[i]);
		t := min(t, a[i]);
	end;

	for var i := 0 to m - 1 do
	begin
		read(b[i]);
		k := min(k, b[i]);
	end;

	for var i := 0 to n - 1 do
		for var j := 0 to m - 1 do
		begin
			if (a[i] = b[j]) then
				l := min(l, a[i]);
		end;

	if (l <> 10) then
		writeln(l)
	else begin
		if (k = t) then
			writeln(k)
		else
			writeln(min(k, t), max(k, t));
	end;

end.

program beautifulNumbers;
var a,b: array[1..9] of integer;
    minFirst,minSecond,x,n,m,i,idential: longint;
BEGIN
readln(n,m);
minFirst:=10; minSecond:=10;
for i:=1 to n do begin
 read(x);
 a[x]:=1;
 if (x<minFirst) then minFirst:=x;
end;
for i:=1 to m do begin
 read(x);
 b[x]:=1;
 if (x<minSecond) then minSecond:=x;
end;
if (minFirst>minSecond) then begin
 x:=minFirst; minFirst:=minSecond; minSecond:=x;
end;
idential:=0; i:=1;
while (i<10) and (idential=0) do begin
 if (a[i]=1) and (b[i]=1) then idential:=i else i:=i+1;
end;
if (idential=0) then begin
 writeln(minFirst,minSecond);
end else begin
 writeln(idential);
end;
END.

var a,b:array [1..9] of integer;
    n,m,i,j,mina,min,minb:integer;
begin
  read (n,m);
  mina:=10;
  minb:=10;
  min:=10;
  for i:=1 to n do begin
    read(a[i]);
    if mina> a[i] then mina:=a[i];
    end;
  for j:=1 to m do begin
    read(b[j]);
    if minb> b[j] then minb:=b[j];
    end;
    for i:=1 to m do
      for j:=1 to n do
        if a[j]= b[i] then if (a[j]< min) then min:=a[j];
    if min<>10 then write (min) else
    if mina>minb then write(minb,mina) else
    write(mina,minb);
end.

type mas=array[1..9] of byte;
var a,b:mas;
	n,m,mina,minb,i:byte;
	c,d:set of byte;
	f:boolean;
begin
	readln(n,m);
	f:=true;
	c:=[];
	d:=[];
	for i:=1 to n do
		begin
			read(a[i]);
			c:=c+[a[i]];
		end;
	readln();
	for i:=1 to m do
		begin
			read(b[i]);
			d:=d+[b[i]];
		end;
	readln();
	for i:=1 to 9 do
		if (i in c) and (i in d) then
			begin
				writeln(i);
				f:=false;
				break;
			end;
	if f then
		begin
			mina:=a[1];
			minb:=b[1];
			for i:=2 to n do
				if a[i]<mina then
					mina:=a[i];
			for i:=2 to m do
				if b[i]<minb then
					minb:=b[i];
			if mina<minb then
				writeln(mina,minb)
			else
				writeln(minb,mina);
		end;
end.

var k, i, n, m, ans, mina, minb: integer;
    a, b, c, d: array [1..9] of integer;
begin
readln (n, m);
mina:=999;
minb:=999;
k:=1;
for i:=1 to n do
    begin
    read (a[i]);
    if a[i]<mina then mina:=a[i];
    c[a[i]]:=1;
    end;
for i:=1 to m do
    begin
    read (b[i]);
    if b[i]<minb then minb:=b[i];
    d[b[i]]:=1;
    end;
for ans:=1 to 9 do
    begin
    if (c[ans]=1) and (d[ans]=1) then k:=0;
    if k=0 then break;
    end;
if k=0 then writeln (ans) else
    if mina<=minb then writeln (mina, minb) else
        writeln (minb,mina);
end.

var
n,m:integer;
ar:array[1..9] of integer;
i,ii:integer;
x:integer;
a:integer;
m1,m2:integer;
some:integer;
begin
readln(n,m);
some:=10;
for i:=1 to 9 do ar[i]:=10;
m1:=-1; m2:=-1;
for i:=1 to n do begin
read(x);
ar[i]:=x;
if(m1=-1)or(m1>x)then m1:=x;
end;
for i:=1 to m do begin
read(x);
for ii:=1 to n do
if(x=ar[ii])and(x<some)then some:=x;
if(m2=-1)or(m2>x)then m2:=x;
end;
if(some<>10)then writeln(some) else
if(m1<m2)then writeln((m1*10+m2)) else
if(m1>m2)then writeln((m2*10+m1));

end.

program mfs;
uses math;
var
ta,tb:array[0..100]of boolean;
n,m,i,j,ba,bb:longint;
num1,num2:longint;
a,b:array[0..100]of longint;
begin
readln(n,m);
fillchar(ta,sizeof(ta),false);
for i:=1 to n do read(a[i]);
for i:=1 to n do ta[a[i]]:=true;
fillchar(tb,sizeof(tb),false);
for i:=1 to m do read(b[i]);
for i:=1 to m do tb[b[i]]:=true;
num1:=0;ba:=1231;bb:=1234;
for i:=1 to 9 do 	if ta[i] and tb[i] then begin writeln(i); halt; end;
for i:=1 to 9 do
	begin
	if ta[i] then
		for j:=i+1 to 9 do if tb[j] then begin ba:=i*10+j; break; end;
	if tb[i] then
		for j:=i+1 to 9 do if ta[j] then begin bb:=i*10+j; break end;
	ba:=min(ba,bb);
	if ba<100 then begin writeln(ba); halt; end;
	end;
end.

var
bol:boolean;
ind,n,m,i,x,j:longint;
a,b:array[-100..10000]of longint;
begin
readln(n,m);
for i:=1 to n-1 do read(a[i]);
readln(a[n]);
for i:=1 to m do read(b[i]);

for i:=1 to n-1 do
for j:=i+1 to n do
if a[i]>a[j] then begin
    x:=a[i];
    a[i]:=a[j];
    a[j]:=x;
end;

for i:=1 to m-1 do
for j:=i+1 to m do
if b[i]>b[j] then begin
    x:=b[i];
    b[i]:=b[j];
    b[j]:=x;
end;

for i:=1 to n do begin
for j:=1 to m do if a[i]=b[j] then begin
    bol:=true;
    ind:=i;
    break;
end;
if bol then break;
end;
if bol then writeln(a[ind])

else begin
    if a[1]<b[1] then write(a[1],b[1])
    else
    write(b[1],a[1]);
end;
end.

program hrn;
 var
  a,b,fa,fb:array[1..9]of byte;
  n,m,i,f,x,y:byte;
 begin
  read(n,m);
  for i:=1 to n do begin
   read(a[i]);fa[a[i]]:=1;end;
  for i:=1 to m do begin
   read(b[i]);fb[b[i]]:=1;end;
  for i:=1 to 9 do
   if(fa[i]=1)and(fb[i]=1)then begin
    write(i);f:=1;break;end;
  if f=0 then begin
   x:=10;y:=10;
   for i:=1 to n do if a[i]<x then x:=a[i];
   for i:=1 to m do if b[i]<y then y:=b[i];
   if x>y then begin
    x:=x+y;
    y:=x-y;
    x:=x-y;
   end;
   write(x,y);
  end;
 end.

program marvel;

var
  a:array[1..10] of Integer;
  b:array[1..10] of Integer;
  i,l,k,n,m,k1,x: Integer;
begin
  Read(n,m);
  k1:=10;
  k:=10;
  for i:=1 to n do
  begin
   Read(a[i]);
   if a[i]<k  then k:=a[i];
   end;
   for l:=1 to m do
   begin
   read(b[l]);
   if b[l] < k1 then k1:=b[l];
   end;
   x:=10;
   for i:=1 to n  do
    for l:=1 to m  do
      if (a[i] = b[l]) and (a[i]< x) then x:=a[i];
   if  x< 10 then writeLn(x)
   else
   begin
   if k1<k then
    WriteLn(k1,k)
  else
  WriteLn(k,k1);
  end;
end.

Program P1;
var n,m,i,a,b,i2,e,e2: integer; a1,a2: array [0..1000] of integer;
begin
a:=9; b:=9; e:=9;
readln(n,m);
for i:= 1 to n do
read(a1[i]);

for i:= 1 to m do
read(a2[i]);

for i:= 1 to n do begin
if a1[i]<a then a:=a1[i];

for i2:= 1 to m do
if (a2[i2]=a1[i]) and (a2[i2]<e) then e:=a2[i2];
end;
if e=9 then begin
for i:= 1 to m do
if a2[i]<b then b:=a2[i];
end;
if e<9 then
write(e) else
write(a,b);
end.

var a, b, c, d, e, f, g, h, z, x, mina, minb: integer;
q, w: array[1..10] of integer;
begin
read(a, b);
e:= 10;
mina:= 9;
minb:= 9;

for g:= 1 to a do
read(q[g]);

for h:= 1 to b do
read(w[h]);

for c:= 1 to a do
begin
for d:= 1 to b do
begin
if (q[c] = w[d]) and (e > q[c]) then
begin
e:= q[c];
write(e);
end;

end;
end;
if (e = 10) then
begin
for z:= 1 to a do
begin
if (mina > q[z]) then
mina:= q[z];
end;
for x:= 1 to b do
begin
if (minb > w[x]) then
minb:= w[x];
end;

if (minb > mina) then
write(mina, minb)
else
write(minb, mina);
end;
end.

var i,n,m,minn,minm:integer;
    a,b:array[1..9]of integer;
begin
  readln(n,m);
  minn:=9;
  minm:=9;
  for i:=1 to n do
    begin
      read(a[i]);
      if a[i]<minn then minn:=a[i];
    end;
  for i:=1 to m do
    begin
      read(b[i]);
      if b[i]<minm then minm:=b[i];
    end;
  if minn<minm then writeln(minn,minm);
  if minn>minm then writeln(minm,minn);
  if minn=minm then writeln(minn);
end.

var
  n, m, i, j, min1, min2, j1, same: integer;
  a, b: array [1..9] of integer;

begin
  same := 10;
  min1 := 10;
  min2 := 10;
  read(n, m);

  for i := 1 to n do
  begin
    read(a[i]);
    if a[i] < min1 then min1 := a[i];
  end;

  for j := 1 to m do
  begin
    read(b[j]);
    if b[j] < min2 then min2 := b[j];
    for j1 := 1 to n do
      if a[j1] = b[j] then
      begin
         if b[j] < same then same:= b[j];

      end;
  end;

  if same <> 0 then writeln(same)
  else if min1 = min2 then writeln (min1)
  else if min1 < min2 then writeln(min1, min2)
  else if min1 > min2 then writeln(min2, min1)

end.

program niceCount;
var
  n, m, i, x, y: integer;
  arrn, arrm: array of integer;
begin
  read(n, m);
  setLength(arrm, m);
  setLength(arrn, n);
  x:= 10;
  y:= 10;
  for i:= 0 to n-1 do read(arrn[i]);
  for i:= 0 to m-1 do read(arrm[i]);
  for i:= 0 to n-1 do
    if arrn[i]<x then x:= arrn[i];
  for i:= 0 to m-1 do
    if arrm[i]<y then y:= arrm[i];
  if (x<y)and(x<>0)or(y=0)and(x<>0) then write(x, y)
  else if (y<x)and(y<>0)or(x=0)and(y<>0) then write(y, x)
  else write(y);
  read(n);
end.

program chisla;

var
  n, m, min1,min2,mind,j,i: integer;
  a: array[1..9] of integer;
  b: array[1..9] of integer;

begin
  Read(n, m);
  for i := 1 to n do
    Read(a[i]);
    for i := 1 to m do
   Read(b[i]);
  min1:=10;
  min2:=10;
  mind:=10;
  for i:=1 to n do
  if a[i]<min1 then
  min1:=a[i];
   for i:=1 to m do
  if b[i]<min2 then
  min2:=b[i];
  for i:=1 to n do
  for j:=1 to n do
  if (a[i]=b[j])and(a[i]<mind) then
  mind:=a[i];
  if mind<10 then
  Write(mind)
  else if min1<min2 then
  Write(min1*10+min2)
  else
  Write(min2*10+min1);
end.

var
n,m,i,j,min1,min2:integer;
a:array[1..100] of integer;
b:array[1..100] of integer;
begin
min1:=10;
min2:=10;
read(n);
read(m);
for i:=1 to n do
read(a[i]);
for j:=1 to m do
read(b[j]);
for i:=1 to n do
if (a[i]<min1) then min1:=a[i];
for j:=1 to m do
if (b[j]<min2) then min2:=b[j];
if (min1=min2) then write(min1)
else begin
if (min1<min2) then write(min1*10+min2)
else writeln(min2*10+min1);
end;
end.

program Kras;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
   n, m, i, k, x, v, z, c: integer;
   a, b: array[1..10] of integer;

begin
   readln(n, m);
   for i := 1 to n do
      read(a[i]);
   for i := 1 to m do
      read(b[i]);
   k := 0;
   while(1 = 1)do
   begin
      inc(k);
      x := k;
      z := 0;
      c := 0;
      while(x <> 0)do
      begin
         v := x mod 10;
         for i := 1 to n do
            if (v = a[i])then
            begin
               z := 1;
               break;
            end;
         x := x div 10;
         if (z = 1)then break;
      end;
      x := k;
      while(x <> 0)do
      begin
         v := x mod 10;
         for i := 1 to n do
            if (v = b[i])then
            begin
               c := 1;
               break;
            end;
         x := x div 10;
         if (c = 1)then break;
      end;
      if ((z = 1) and (c = 1))then break;
   end;
   writeln(k);
end.

program n1;
var q,n,m,i,u,amin,bmin,cmin,t:integer;
    a,b,c: array [1..9] of integer;
begin;
read (n,m);
readln;
for i:=1 to n do
read (a[i]);
readln;
for i:=1 to n do
read (b[i]);
for i:=1 to n do

q:=1;
for i:=1 to n do
 for u:=1 to m do
  if (a[i]=b[u]) then
   begin
   c[q]:=a[i];
   q:=q+1;
   end;

cmin:=c[1];
if (cmin>a[q]) then amin:=c[i];

if (cmin>0)
then t:=cmin
  else
  begin
amin:=a[1];
if (amin>a[i]) then amin:=a[i];
bmin:=b[1];
for i:=1 to m do
if (bmin>b[i]) then bmin:=b[i];
if (amin=bmin) then
t:=amin;
if (amin>bmin) then
t:=bmin*10+amin;
if (amin<bmin) then
t:=bmin+amin*10;
end;
writeln (t);
end.

var s,n,m,i,j:integer;
    a,b:array of integer;
begin
read(n,m);
SetLength(a,n);
SetLength(b,m);
for i:=0 to n-1 do read(a[i]);
for j:=0 to m-1 do read(b[j]);
s:=a[0]*10 + b[0];
for i:= 0 to n-1 do
begin
for j:= 0 to m-1 do
begin
if (a[i]*10+b[j] < s) and (b[j]*10 +a[i] > a[i]*10 + b[j])then s:=a[i]*10+b[j]
else if (b[j]*10 +a[i] < a[i]*10 + b[j]) then s:=b[j]*10 + a[i];
if (a[i]=b[j]) and (a[i]<s) then s:=a[i];
end;
end;
write(s);
end.

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Math;

var
  mi1, mi2, i, j, n, m: integer;
  a, b: array [1..100] of integer;

begin
  read(n, m);
  for i := 1 to n do
    read(a[i]);
  for i := 1 to m do
    read(b[i]);
  for i := 1 to n do
    for j := 1 to m do
      if (a[i] = b[j]) then begin
        write(a[i]);
        exit;
      end;
  mi1 := a[1];
  for i := 2 to n do
    if (a[i] < mi1) then
      mi1 := a[i];
  mi2 := b[1];
  for j := 1 to m do
    if (b[j] < mi2) then
      mi2 := b[j];
  write(min(mi1, mi2), max(mi1, mi2));
  readln;
  readln;
end.

var n,m,a,i,j,min1,min2,min: byte;
arr1, arr2 : array[1..10] of byte;
begin
readln(n, m);
min1:=10;
min2:=10;
min:=10;
for i:=1 to n do
  read(arr1[i]);

for i:=1 to m do
  read(arr2[i]);

if n<m then begin
             for i:=1 to n do
               for j:=1 to n do
                  if (arr1[i] = arr2[j]) and (min > arr2[j]) then min:=arr1[i];
            end;

if n>m then begin
             for i:=1 to m do
               for j:=1 to m do
                  if (arr2[i] = arr1[j]) and (min > arr1[j]) then min:=arr2[i];
            end;

if min=10 then begin
                  for i:=1 to m do
                    if arr2[i] < min2 then min2:=arr2[i];
                  for i:=1 to n do
                    if arr1[i] < min1 then min1:=arr1[i];
                  if min2 > min1 then write(min1,min2) else write(min2,min1);
                 end
                 else
                 write(min);
end.

type mas=array[1..10] of integer;
var

a,b:mas;
flag,flag2:boolean;
i,j,m,n:integer;

BEGIN
  readln(n, m);
  for i:=1 to n do
    begin
      read(a[i]);
    end;
  for i:=1 to m do
    begin
      read(b[i]);
    end;
  for i:=1 to 98 do
    begin
      if i>=10 then
        begin
          flag:=false;
          for j:=1 to n do
            begin
              if a[j]=i mod 10 then flag:=true;
            end;
          flag2:=false;
          for j:=1 to m do
            begin
              if a[j]=i div 10 then flag2:=true;
            end;
          if flag and flag2 then
            begin
              writeln(i);
              break;
            end;
        end
      else
        begin
          flag:=false;
          for j:=1 to n do
            begin
              if a[j]=i then flag:=true;
            end;
          flag2:=false;
          for j:=1 to m do
            begin
              if a[j]=i then flag2:=true;
            end;
          if flag and flag2 then
            begin
              writeln(i);
              break;
            end;
        end;
    end;
END.

program z1;
var a,b:array[1..9]of integer;i,m,n,p,k,h:integer;
procedure QuickSort( L, R : Integer ); { Быстрая сортировка массива A[] }
var i,j,x,y,m: integer;
begin
  i := l; j := r;
  x := a[(l+r) div 2];
  repeat
    while (A[i] < x) do inc(i);
    while (x < A[j]) do dec(j);
    if ( i <= j ) then
    begin
      y:=A[i]; a[i]:=a[j]; a[j]:=y;
            inc(i); dec(j);
    end;
  until (i > j);
  if (l < j) then QuickSort(l,j);
  if (i < r) then QuickSort(i,r);
end;
procedure QuickSort1( L, R : Integer ); { Быстрая сортировка массива A[] }
var i,j,x,y,m: integer;
begin
  i := l; j := r;
  x := b[(l+r) div 2];
  repeat
    while (b[i] < x) do inc(i);
    while (x < b[j]) do dec(j);
    if ( i <= j ) then
    begin
      y:=b[i]; b[i]:=b[j]; b[j]:=y;
            inc(i); dec(j);
    end;
  until (i > j);
  if (l < j) then QuickSort(l,j);
  if (i < r) then QuickSort(i,r);
end;
function min(k,h:integer):integer;
begin
if k>h then min:=h
else min:=k;end;
begin
read(n,m);
for i:=1 to n do
read(a[i]);quicksort(1,n);
for i:=1 to m do read(b[i]);
quicksort1(1,m);
k:=a[1]*10+b[1];
h:=b[1]*10+a[1];
p:=min(k,h);

for i:=1 to n do
if a[i]=b[i] then if a[i]<p then p:=a[i];
write(p);
end.

var a,b:array[1..9] of longint;
i,j,m,n,k:longint;
function min(a,b:longint):longint;
begin
min:=a;
if a>b then min:=b;
end;
BEGIN
read(m,n);
k:=100;
for i:=1 to m do
read(a[i]);
for j:=1 to n do
read(b[i]);
for i:=1 to m do
for j:=1 to n do
begin
if a[i]<>0 then k:=min(k,a[i]*10+b[j]);
if b[j]<>0 then k:=min(k,b[j]*10+a[i]);
if a[i]=b[j] then k:=min(k,a[i]);
end;
write(k)
end.

var
  n, m, i,  j, min1, min2, k: integer;
  a: array [1..9] of integer;
  b: array [1..9] of integer;

begin
  read(n, m);
  read(a[1]);
  min1 := a[1];
  for i := 2 to n do
  begin
    read(a[i]);
    if a[i] < min1 then
      min1 := a[i];
  end;
  read(b[1]);
  min2 := b[1];
  for i := 2 to m do
  begin
    read(b[i]);
    if b[i] < min2 then
      min2 := b[i];
  end;
  k := 10;
  for i := 1 to n do
    for J := 1 to m do
      if (a[i] = b[j]) and (k > a[i]) then
        k := a[i];
  if (k < 10) then
    writeln(k)
  else
    writeln(min1, min2);
end.

var a,b:array[1..10]of integer;
n,i,k,m,j,sw:integer;
begin
     readln(n,m);
     for i:=1 to n do
         read(a[i]);
     for i:=1 to m do
         read(b[i]);
     for j:=1 to n-1 do
         for i:=1 to n-1 do
             if a[i] > a[i+1] then
             begin
                  sw:=a[i];
                  a[i]:=a[i+1];
                  a[i+1]:=sw;
             end;
     for j:=1 to m-1 do
         for i:=1 to m-1 do
             if b[i] > b[i+1] then
             begin
                  sw:=b[i];
                  b[i]:=b[i+1];
                  b[i+1]:=sw;
             end;
     i:=1;
     if a[1] > b[1] then
     begin
        while (a[i] <> b[1])and(i < n) do
              i:=i+1;
        if i <> 1 then
              if i <> n then
                 k:=b[1]
              else
                  k:=b[1]*10+a[1]
        else
            if a[1] = b[1] then
               k:=a[1];

     end
     else
     begin
        while (a[1] <> b[i])and(i < m) do
              i:=i+1;
        if i <> 1 then
           if i <> m then
              k:=a[1]
           else
               k:=a[1]*10+b[1]
         else
            if a[1] = b[1] then
               k:=a[1];

     end;
     writeln(k);
end.

Program cg;
    var n,m,i,a,mina,minb,b,k:integer;
    begin
read(n);
readln(m);
mina:=10;
minb:=10;
For i:=1 to n do begin
          read(a);
          if a<mina then mina:=a;
          end;
          readln;
For i:=1 to m do begin
          read(b);
          if b<minb then minb:=b;
          end;
if mina>minb then k:=minb*10+mina;
if minb>mina then k:=mina*10+minb;
if mina=minb then k:=mina;
writeln(k);
end.

Program cg;
    var n,m,i,a,mina,minb,b,k:integer;
    begin
read(n);
readln(m);
mina:=9;
minb:=9;
For i:=1 to n do begin
          read(a);
          if a<mina then mina:=a;
          end;
For i:=1 to m do begin
          read(b);
          if b<minb then minb:=b;
          end;
if mina>minb then k:=minb*10+mina;
if minb>mina then k:=mina*10+minb;
if mina=minb then k:=mina;
writeln(k);
end.

program Ann;
var n,m,min1,min2,i,j:integer;
a,b:array[1..10]of integer;
begin
readln(n,m);
min1:=357;
min2:=357;
for i:=1 to n do
    begin
    read(a[i]);
    if(a[i] < min1)then
            min1:=a[i];
    end;
for i:=1 to m do
    begin
    read(b[i]);
    if(b[i] < min2)then
            min2:=b[i];
    end;
if(min1 = min2)then
        writeln(min1)
else
if(min1 < min2)then
        if(min1 = 0)then
                writeln(min2)
        else
            writeln(min1,min2)
else
        if(min2 = 0)then
                writeln(min1)
        else
            writeln(min2,min1);
end.

var n,m,a,i,j,min1,min2,min: byte;
arr1, arr2 : array[1..10] of byte;
begin
readln(n, m);
min1:=10;
min2:=10;
min:=10;
for i:=1 to n do
  read(arr1[i]);

for i:=1 to m do
  read(arr2[i]);

if n<m then begin
             for i:=1 to n do
               for j:=1 to n do
                  if (arr1[i] = arr2[j]) and (min > arr2[j]) then min:=arr1[i];
            end;

if n>m then begin
             for i:=1 to m do
               for j:=1 to m do
                  if (arr2[i] = arr1[j]) and (min > arr1[j]) then min:=arr2[i];
            end;

if n=m then begin
             for i:=1 to m do
               for j:=1 to n do
                  if (arr2[i] = arr1[j]) and (min > arr1[j]) then min:=arr2[i];
            end;

if min=10 then begin
                  for i:=1 to m do
                    if arr2[i] < min2 then min2:=arr2[i];
                  for i:=1 to n do
                    if arr1[i] < min1 then min1:=arr1[i];
                  if min2 > min1 then write(min1,min2) else write(min2,min1);
                 end
                 else
                 write(min);
end.

var a,b:array [1..9] of integer;
    n,m,i,j,mina,min,minb:integer;
begin
  read (n,m);
  mina:=10;
  minb:=10;
  min:=10;
  for i:=1 to n do begin
    read(a[i]);
    if mina> a[i] then mina:=a[i];
    end;
  for j:=1 to m do begin
    read(b[j]);
    if minb> b[j] then minb:=b[j];
    end;
    for i:=1 to m do
      for j:=1 to n do
        if a[j]= b[i] then if (a[j]< min) then min:=a[j];
    if min<>10 then write (min) else
    write(mina,minb);
end.

	var n, m,min1,min2,i:integer;
  a:array[1..10] of integer;
	b:array[1..10] of integer;

	begin
	min1:=10;min2:=10;
	read(n,m);
	for i:=1 to n do
       (read(a[i]));
	for i:=1 to m do
       (read(b[i]));
	for i:=1 to n do
        if (a[i]<min1)then
            min1:=a[i];
    for i:=1 to m do
        if (b[i]<min2)then
            min2:=b[i];
    if (min1=min2)then
        write(min1)
        else
            write(min(min1,min2),max(min1,min2));
  end.

Uses math;
Const
        fin='';
        fon='';
        maxC=1000000000;
        maxN=1000+10;
	module=1000000007;

Type
	re=record
		x,y:longint;
		end;

Var
        fi,fo:text;
	n,mi,mii,m:longint;
        a,b:array[0..maxN] of longint;

Procedure mo;
Begin
        Assign(fi,fin); Reset(fi);
        Assign(fo,fon); Rewrite(fo);
End;

Procedure dong;
Begin
        Close(fi);
        Close(fo);
End;

Procedure nhap;
Var
	i:longint;
Begin
        mii:=10;
        mi:=10;
        Readln(fi,n,m);
        For i:=1 to n do
        Begin
                Read(fi,a[i]);
                mi:=min(mi,a[i]);
        End;
        For i:=1 to m do
        Begin
                Read(fi,b[i]);
                mii:=min(mii,b[i]);
        End;
End;

Procedure xuly;
Var
        i,j:longint;
Begin
        If mi=mii then write(fo,mi) else
        Write(fo,min(mi,mii),max(mi,mii));
End;

Procedure xuat;
Begin
End;

BEGIN
        Mo;
        Nhap;
        Xuly;
        Xuat;
        Dong;
END.

var
  n, m, min1, min2, i, x: byte;

begin
  read(n, m);
  read(min1);
  for i := 2 to n do
  begin
    read(x);
    if x < min1 then min1 := x;
  end;
  read(min2);
  for i := 2 to m do
  begin
    read(x);
    if x < min2 then min2 := x;
  end;
  if min1 < min2 then write(min1, min2)
  else if min2 < min1 then write(min2, min1)
  else write(min1);
end.

	var n, m,min1,min2,i,min3,j:integer;
  a:array[1..10] of integer;
	b:array[1..10] of integer;

	begin
	min1:=10;min2:=10;min3:=10;
	read(n,m);
	for i:=1 to n do
       (read(a[i]));
	for i:=1 to m do
       (read(b[i]));
	for i:=1 to n do
        if (a[i]<min1)then
            min1:=a[i];
    for i:=1 to m do
        if (b[i]<min2)then
            min2:=b[i];
    for i:=1 to n do
      for j:=1 to m do
        if a[i]=b[j] then
          min3:=a[i];
        if min3<>10 then
          write(min3)
          else
            write(min(min1,min2),max(min1,min2));
  end.

Program sdfg;
     var mina, minb,i,k,minc,n,m:integer;
        A: array [1..9] of integer;
        B: array [1..9] of integer;
    begin
mina:=9;
minb:=9;
minc:=10;
read(n);
readln(m);
   for i:= 1 to n do begin
       read(a[i]);
       if a[i]<mina then mina:=a[i];
       end;
       readln;
   for i:= 1 to m do begin
       read(b[i]);
       if b[i]<minb then minb:=b[i];
       end;
 for i:= 1 to n do begin
      for k:= 1 to m do
          if (a[i]=b[k]) and (a[i]<minc) then minc:=a[i];
          end;
          if minc=10 then minc:=100;
   if mina<minb then k:=mina*10+minb;
    if minb<mina then k:=minb*10+mina;
    if k<minc then writeln(k)
      else writeln(minc);
      end.

var
  n, m, i,  j, min1, min2, k, l: integer;
  a: array [1..9] of integer;
  b: array [1..9] of integer;

begin
  read(n, m);
  L := 0;
  if (n <= 9) and (n >= 1) and (m >= 1) and (m <= 9) then
  begin
    min1 := 10;
    for i := 1 to n do
    begin
      read(a[i]);
      if (a[i] > 9) or (a[i] < 1) then
      begin
        l := 1;
        break;
      end;
      if a[i] < min1 then
        min1 := a[i];
    end;
    if l = 0 then
    begin
      min2 := 10;
      for i := 1 to m do
      begin
        read(b[i]);
        if (b[i] > 9) or (b[i] < 1) then
        begin
          l := 1;
          break;
        end;
        if b[i] < min2 then
          min2 := b[i];
      end;
      if l = 0 then
      begin
        k := 10;
        for i := 1 to n do
          for J := 1 to m do
            if (a[i] = b[j]) and (k > a[i]) then
              k := a[i];
        if (k < 10) then
          writeln(k)
        else
          writeln(min1, min2);
      end;
    end;
  end;
end.

program z1;
var a,b:array[1..9]of integer;i,m,n,p,k,h,j:integer;
procedure QuickSort( L, R : Integer ); { Быстрая сортировка массива A[] }
var i,j,x,y,m: integer;
begin
  i := l; j := r;
  x := a[(l+r) div 2];
  repeat
    while (A[i] < x) do inc(i);
    while (x < A[j]) do dec(j);
    if ( i <= j ) then
    begin
      y:=A[i]; a[i]:=a[j]; a[j]:=y;
            inc(i); dec(j);
    end;
  until (i > j);
  if (l < j) then QuickSort(l,j);
  if (i < r) then QuickSort(i,r);
end;
procedure QuickSort1( L, R : Integer ); { Быстрая сортировка массива A[] }
var i,j,x,y,m: integer;
begin
  i := l; j := r;
  x := b[(l+r) div 2];
  repeat
    while (b[i] < x) do inc(i);
    while (x < b[j]) do dec(j);
    if ( i <= j ) then
    begin
      y:=b[i]; b[i]:=b[j]; b[j]:=y;
            inc(i); dec(j);
    end;
  until (i > j);
  if (l < j) then QuickSort(l,j);
  if (i < r) then QuickSort(i,r);
end;
function min(k,h:integer):integer;
begin
if k>h then min:=h
else min:=k;end;
begin
read(n,m);
for i:=1 to n do
read(a[i]);quicksort(1,n);
for i:=1 to m do read(b[i]);
quicksort1(1,m);
k:=a[1]*10+b[1];
h:=b[1]*10+a[1];
p:=min(k,h);
for j:=1 to n do
for i:=1 to n do
if a[j]=b[i] then if a[i]<p then p:=a[j];
write(p);
end.

var
  a, b: array [0..1000] of integer;
  i, j, n, m, q, w: integer;

begin
  read(n, m);
  readln;
  for i := 0 to n - 1 do
  begin
    read(a[i]);
  end;
  readln;
  for j := 0 to m - 1 do
  begin
    read(b[j]);
  end;
  q := 333;
  w := 333;
  for i := 0 to n - 1 do
  begin
    if(a[i] < q) then q := a[i];
  end;
  for j := 0 to m - 1 do
  begin
    if(b[j] < w) then w := b[j];
  end;

  if(q = w) then begin
    write(q)
  end;

  if(q < w) then begin
    write(10*q+w);
  end;

  if(q > w) then begin
    write(10*w+q);
  end;
end.

program ne3;
var i, n,a,b, m,mina,minb:integer;

begin
read(n,m);
mina:=9;
minb:=9;
for i:=1 to n do
begin
read(a);
if a<mina then mina:=a;
end;
for i:=1 to m do
begin
read(b);
if b<minb then minb:=b;
end;
if mina=minb then writeln(mina)
else
if mina>minb then writeln(minb*10+mina)
else
if mina<minb then writeln(mina*10+minb);
end.

program kubai;

{$APPTYPE CONSOLE}

uses
  SysUtils, Math;

const
  INF = 100;

var
  n, m, i, j, k, min1, min2, x: integer;
  a, b: array[1..10] of integer;

begin
  readln(n, m);
  min1 := INF;
  min2 := INF;
  for i := 1 to n do begin
    read(a[i]);
    if (a[i] < min1) then
      min1 := a[i];
  end;
  for i := 1 to m do begin
    read(b[i]);
    if (b[i] < min2) then
      min2 := b[i];
  end;

  x := INF;
  for i := 1 to n do
    for j := 1 to m do
      if (a[i] = b[j]) and (a[i] < x) then
        x := a[i];

  if (x <> 0) then
    writeln(x)
  else
  if (min1 = min2) then
    writeln(min1)
  else
    writeln(min(min1, min2), max(min1, min2));   readln;readln;
end.

program x1;
var q,n,m,min1,min2,i:integer;
begin
readln(n,m);
read(q);
min1:=q;
for i:=1 to n-1 do begin
read(q);
if q<min1 then min1:=q;
end;
read(q);
min2:=q;
for i:=1 to m-1 do begin
read(q);
if q<min2 then min2:=q;
end;
if min1=min2 then write(min1) else if min1>min2 then write(min2,min1) else write(min1,min2);
end.

var
s,c:string;
a,b:array[1..10]of integer;
n,m,i,mina,minb,j:integer;
begin
read(n,m);
mina:=9;
minb:=9;
for i:= 1 to n do begin
read(a[i]);
if a[i]<mina then mina:=a[i];
end;
for i:= 1 to n do begin
read(b[i]);
if b[i]<minb then minb:=b[i];
end;
for i:= 1 to n do
 for j:= 1 to n do
 if a[i]=b[j] then begin writeln(a[i]);exit;end;
if mina=minb then begin writeln(mina);exit;end;
if mina>minb then begin str(mina,s);str(minb,c);writeln(c+s);exit;end;
if mina<minb then begin str(mina,s);str(minb,c);writeln(s+c);exit;end;
writeln(s+c);
end.

program technocup_1;
var
x,y,i,min1,min2:integer;
x1,x2:array of integer;
begin
read(x,y);
SetLength(x1,x);
SetLength(x2,y);
read(x1[0]);
min1:=x1[0];
for i := 1 to x-1 do
begin
  read(x1[i]);
  if x1[i] < min1 then
    min1 := x1[i];
end;
read(x2[0]);
min2:=x2[0];
for i := 1 to y-1 do
begin
  read(x2[i]);
  if x2[i] < min2 then
    min2 := x2[i];
end;
if min1 < min2 then
  write(min1*10 + min2)
else if min2 < min1 then
  write(min2 * 10 + min1)
else
  write(min1);
end.

var i,n,m,min1,min2,a,b : integer;
begin
min1:=10;
min2:=10;
read(n);
read (m);
readln;
for i:=1 to n do
begin
read(a);
if (a<min1) and (a>0) then
min1:=a;
end;
for i:=1 to m do
begin
read(b);
if (b<min2) and (b>0) then
min2:=b;
end;
if min1=min2 then
write(min1);
if min1>min2 then
write (min2,min1);
if min1<min2 then
write (min1,min2);
end.

var min2,min,n,m,i:integer;
a:array[1..10] of integer;
b:array[1..10]  of integer;
begin
read(n);
read(m);
for i:=1 to n do
  read(a[i]);
for i:=1 to m do
  read(b[i]);
min:=a[1];
min2:=b[1];
for i:=1 to n do
    if a[i]<min then min:=a[i];
for i:=1 to m do
    if b[i]<min2 then min2:=b[i];
if min=min2 then writeln(min)
  else
    begin
      if min>min2 then
        begin
          min:=min2-min;
          min2:=min2-min;
          min:=min2+min;
        end;
          writeln(min*10+min2);
    end;
end.

Program cg;
    var n,m,i,a,mina,minb,b,k:integer;
    begin
read(n);
readln(m);
mina:=10;
minb:=10;
For i:=1 to n do begin
          read(a);
          if a<mina then mina:=a;
          end;
For i:=1 to m do begin
          read(b);
          if b<minb then minb:=b;
          end;
if mina>minb then k:=minb*10+mina;
if minb>mina then k:=mina*10+minb;
if mina=minb then k:=mina;
writeln(k);
end.

var
a,b:array [1..9] of integer;
m,n,i,j,m1,m2,x:integer;
q:boolean;
begin
m1:=10;
m2:=10;
readln(n,m);
q:=false;
for i:=1 to n do
read(a[i]);
for i:=1 to m do
read(b[i]);
for i:=1 to n do
begin
for j:=1 to m do
if (a[i]=b[j]) and (a[i]<m1) then
begin
q:=true;
m1:=a[i];
end;
end;
if q=false then
begin
for i:=1 to n do
if a[i]<m2 then
m2:=a[i];
m1:=10;
for i:=1 to m do
if b[i]<m1 then
m1:=b[i];
writeln(max(m1,m2)*10+min(m1,m2));
end
else
writeln(m1);
end.

var a,b: array[1..9] of integer;
n,m,amin,bmin,i:integer;
begin
amin:=9;
bmin:=9;
readln(n,m);
for i:=1 to n do begin
read(a[i]);
end;
for i:=1 to m do begin
read(b[i]);
end;
for i:=1 to n do begin
if a[i]<=amin then
amin:=a[i];
end;

for i:=1 to m do begin
if b[i]<=bmin then
bmin:=b[i];
end;

if amin=bmin then begin
writeln(amin);
end
else
begin
if amin>bmin then begin
writeln(10*bmin+amin);
end
else
begin
writeln(10*amin+bmin);
end;
end;

end.

var
n,m,i,j,min1,min2:integer;
a:array[1..100] of integer;
b:array[1..100] of integer;
begin
min1:=10;
min2:=10;
read(n);
read(m);
for i:=1 to n do
read(a[i]);
for j:=1 to m do
read(b[j]);
for i:=1 to n do
if (a[i]<min1) then min1:=a[i];
for j:=1 to m do
if (b[j]<min2) then min2:=b[j];
if (min1=min2) then write(min1)
else begin
if (min1<min2) then write(min1*10+min2)
else writeln(min2*10+min1);
end;
end.

var a,b,a1,a2,b1,i:integer;
begin
read(a,b);
read(a1);
for i:=1 to a-1 do
begin
read(b1);
if b1<a1 then a1:=b1;
end;
read(a2);
for i:=1 to a-1 do
begin
read(b1);
if b1<a2 then a2:=b1;
end;

if a1=a2 then write(a1) else
if a1<a2 then write(a1,a2) else write(a2,a1);
end.

program z1;
var a,b:array[1..9]of integer;i,m,n,p,k,h,j:integer;
procedure QuickSort( L, R : Integer ); { Быстрая сортировка массива A[] }
var i,j,x,y,m: integer;
begin
  i := l; j := r;
  x := a[(l+r) div 2];
  repeat
    while (A[i] < x) do inc(i);
    while (x < A[j]) do dec(j);
    if ( i <= j ) then
    begin
      y:=A[i]; a[i]:=a[j]; a[j]:=y;
            inc(i); dec(j);
    end;
  until (i > j);
  if (l < j) then QuickSort(l,j);
  if (i < r) then QuickSort(i,r);
end;
procedure QuickSort1( L, R : Integer ); { Быстрая сортировка массива A[] }
var i,j,x,y,m: integer;
begin
  i := l; j := r;
  x := b[(l+r) div 2];
  repeat
    while (b[i] < x) do inc(i);
    while (x < b[j]) do dec(j);
    if ( i <= j ) then
    begin
      y:=b[i]; b[i]:=b[j]; b[j]:=y;
            inc(i); dec(j);
    end;
  until (i > j);
  if (l < j) then QuickSort(l,j);
  if (i < r) then QuickSort(i,r);
end;
function min(k,h:integer):integer;
begin
if k>h then min:=h
else min:=k;end;
function max(n,m:integer):integer;
begin
if n<m then max:=m
else max:=n;end;
begin
read(n,m);
for i:=1 to n do
read(a[i]);quicksort(1,n);
for i:=1 to m do read(b[i]);
quicksort1(1,m);
k:=a[1]*10+b[1];
h:=b[1]*10+a[1];
p:=min(k,h);
for j:=1 to n do
for i:=1 to m do
if a[j]=b[i] then if a[i]<p then p:=a[j];
write(p);
end.

program codechef;
type mas=array[1..100] of integer;
var a,b:mas;
n,m,i,k1,k2:integer;

function    min(var a:mas; k:longint):longint;
var i,l:longint;
begin

    l:=1;
    for i:=2 to k do
        if (a[i])<a[l]
            then
                l:=i;
    min:=a[l];

end;
begin

  readln(n,m);
  For i:=1 to n do
      read(a[i]);
  For i:=1 to m do
      read(b[i]);
  k1:=min(a,n);
  k2:=min(b,m);
  if k1 < k2
      then
          write(k1*10+k2)
      else
          if k1=k2
            then
              write(k1)
            else
              write(k2*10+k1);
end.

var n,m,i,j,min:integer;
    a,b:array[1..10] of byte;
begin
  readln(n,m);
  for i:=1 to n do read(a[i]);
  for i:=1 to m do read(b[i]);
  min:=100;
  for i:=1 to n do
    for j:=1 to m do
     if a[i]*10+b[j]<min then min:=a[i]*10+b[j];
  if (min mod 10)=(min div 10) then min:=min mod 10;
  writeln(min);
end.

var
   n,m,i,j,z,min1,min2,itog1,itog2 : longint;
   n1,m1 : Array of integer;

begin
      readln(n,m);
      SetLength(n1,n);
      SetLength(m1,m);

      for i := 0 to n-1 do
      begin
            read(n1[i]);
      end;
      for i := 0 to m-1 do
      begin
            read(m1[i]);
      end;

      for i := 0 to n-1 do
      begin
            for j := 0 to m-1 do
            begin
                 if n1[i] = m1[j] then
                 begin
                      writeln(n1[i]);
                      exit;
                 end;
            end;
      end;
      min1 := 10;
      min2 := 10;
      for i := 0 to n-1 do
      begin
            if n1[i] < min1 then
              min1 := n1[i]
      end;
      for j := 0 to m-1 do
      begin
            if m1[j] < min2 then
              min2 := m1[j]
      end;
      itog1 := (min1 * 10) + min2;
      itog2 := (min2 * 10) + min1;

      if itog1 < itog2 then
         writeln(itog1)
      else
         writeln(itog2);

end.

program cras;
var n,m,x,i: integer;
    a,b: array [1..9] of integer;
begin
  for i:=1 to 9 do
    begin
      a[i]:=0;
      b[i]:=0;
    end;
  read(n,m);
  for i:=1 to n do
    begin
      read(x);
      a[x]:=1;
    end;
  for i:=1 to m do
    begin
      read(x);
      b[x]:=1;
    end;
  i:=1;
  while(a[i]=0) do i:=i+1;
  x:=1;
  while(b[x]=0) do x:=x+1;
  if x>i then x:=i*10+x
  else if i>x then x:=x*10+i;
  writeln(x);
end.

 var n,m,i,j,z,min1,min2:longint;
     a1,a2:array [1..9] of byte;
begin
 readln(n,m);
 for i:=1 to n do
   read(a1[i]);
 readln;
 min1:=a1[1];
 for i:=2 to n do
   if a1[i]<min1 then
     min1:=a1[i];
 for i:=1 to m do
   read(a2[i]);
 readln;
 min2:=a2[1];
 for i:=2 to m do
   if a2[i]<min2 then
     min2:=a2[i];
 if min1>min2 then
   writeln(min2,min1);
 if min1<min2 then
   writeln(min1,min2);
 if min1=min2 then
   writeln(min1);
end.

var
n,m,min1,min2,a,b,i:integer;
Begin
  read(n,m);
  min1:=20;
  min2:=20;
  for i:=1 to n do
  begin
    read(a);
    if a<min1 then min1:=a;
  end;
  for i:=1 to m do
  begin
    read(b);
    if b<min2 then min2:=b;
  end;
  if (m=1) then writeln(min2)
  else
   if (n=1) then writeln(min1)
   else
   begin
     if min1=min2 then  writeln(min1);
    if (min2 > min1) then writeln(min1*10+min2);
   if   (min2 < min1)then writeln(min2*10+min1);
   end;
End.

var n,m,i,j,outp,b:integer;
a:array[1..9] of integer;
begin
outp:=10;
readln(n,m);
for i:=1 to n do
read(a[i]);
for i:=1 to m do
begin
read(b);
for j:=1 to n do
if b=a[j] then
begin
if b<outp then
outp:=b;
end
else
begin
if (b*10+a[j])<outp then outp:=b*10+a[j];
if (b+a[j]*10)<outp then outp:=b+a[j]*10;
end;
end;
write(outp);
end.

var n,m,i,j,min:longint;
    a,b:array[1..10] of longint;
begin
  readln(n,m);
  for i:=1 to n do read(a[i]);
  for i:=1 to m do read(b[i]);
  min:=100;
  for i:=1 to n do
    for j:=1 to m do begin
     if a[i]*10+b[j]<min then min:=a[i]*10+b[j];
     if (a[i]=b[j]) and (a[i]<min) then min:=a[i]; end;
  writeln(min);
end.

program Ann;
var n,m,min1,min2,i,j:integer;
a,b:array[1..10]of integer;
begin
readln(n,m);
min1:=357;
min2:=357;
for i:=1 to n do
    begin
    read(a[i]);
    if(a[i] < min1)then
            min1:=a[i];
    end;
for i:=1 to m do
    begin
    read(b[i]);
    if(b[i] < min2)then
            min2:=b[i];
    end;
if(min1 = min2)then
        writeln(min1)
else
if(min1 < min2)then
        writeln(min1,min2)
else
        writeln(min2,min1);
end.

var
   i, j, n, m, temp: integer;
   arr1, arr2: array [0..99999] of integer;
Begin
    readln(n, m);
    for i:= 0 to n - 1 do
        begin
             read(arr1[i]);
        end;

    for i:= 0 to m - 1 do
        begin
             read(arr2[i]);
        end;

    for i:= 0 to n - 2 do
        for j:= 0 to n - i do
            if (arr1[j] > arr1[j + 1]) then
               begin
                    temp:= arr1[j];
                    arr1[j]:= arr1[j + 1];
                    arr1[j + 1]:= temp;
               end;

    for i:= 0 to m - 2 do
        for j:= 0 to m - i do
            if (arr2[j] > arr2[j + 1]) then
               begin
                    temp:= arr1[j];
                    arr1[j]:= arr1[j + 1];
                    arr1[j + 1]:= temp;
               end;

    n:= arr1[0];
    m:= arr2[0];

    if (n = m) then writeln(n);
    if (n > m) then writeln(m, n)
    else writeln(n, m);
End.

var n, m, i, j, min1, min2: integer;
begin
   readln(n, m);
   for i:=1 to n do begin
      read(j); if (j<min1) or (min1=0) then min1:=j;
   end;
    for i:=1 to m do begin
      read(j); if (j<min2) or (min2=0) then min2:=j;
   end;
   if min1=min2 then writeln(min1)
   else begin
      if min1<min2 then writeln(min1, min2);
      if min2<min1 then writeln(min2, min1);
   end;
end.

program AM1;
   var    n,m,i,l,k,p,o,j,h: integer;
            v:array[1..9] of integer;

begin
  readln(n,m);
  k:=10;
  for i:=1 to n do
  begin
    read(v[i]);
    if v[i]<k then k:=v[i];
  end;
  for i:=n+1 to 9 do
v[i]:=100;
  l:=10;
  o:=10;
  for i:=1 to m do
  begin
    read(p);
    for j:=1 to n do
     if p=v[j] then
     begin
       h:=0;
       if o>p then o:=p;
     end;
    if v[i]<l then l:=v[i];
  end;
  if h=0 then write(o)
  else
      if k>l then write(10*l+k)
      else
          write(10*k+l);

end.

var m,n,i,min1,min2,x,i1:integer;
a,b:array[1..100] of integer;
begin
x:=10;
 readln(n,m);
 for i:=1 to n do
 read(a[i]);
 for i:=1 to m do
 read(b[i]);
 min1:=a[1];
 min2:=b[1];
 for i:=1 to n do
 begin
  for i1:=1 to m do
  if a[i1]=b[i] then
  if b[i]<x then x:=b[i];
 end;
if x<10 then
begin
writeln(x);
exit;
end;
for i:=2 to n do
 if a[i]<min1 then
 min1:=a[i];
 for i:=2 to m do
 if b[i]<min2 then
 min2:=b[i];
if min1<min2 then
writeln(min1*10+min2)
else
writeln(min2*10+min1);
end.

const
  t = 1000;

type
  Arr = array [1..t] of longint;

var
  a, b: Arr;
  i, j, m, min1, min2, n: longint;

begin
  readln(n, m);
  for i := 1 to n do
    read(a[i]);
  for j := 1 to m do
    read(b[j]);

  min1 := a[1];
  min2 := b[1];

  for i := 2 to n do
    if a[i] < min1 then min1 := a[i];
  for j := 2 to m do
    if b[j] < min2 then min2 := b[j];

  if min1 < min2 then writeln(min1*10+min2)
    else
  if min1 = min2 then writeln(min1)
  else
    writeln(min2*10+min1);
end.

program marvel;

var
  i,l,k,n,m,k1,x: Integer;
begin
  Read(n,m);
  k1:=10;
  k:=10;
  for i:=1 to n do
  begin
   Read(x);
   if x<k  then k:=x;
   end;
   for l:=1 to m do
   begin
   read(x);
   if x < k1 then k1:=x;
   end;
   if k1 = k then writeLn(k)
   else
   begin
   if k1<k then
    WriteLn(k1,k)
  else
  WriteLn(k,k1);
  end;
end.

var n, m, i, minn, minm, a, s: integer;

begin
  readln(n, m);
  minn := 10;
  minm := 10;

  for i := 1 to n do
  begin
    read(a);
    if (a < minn) then minn := a;
  end;

  for i := 1 to m do
  begin
    read(a);
    if (a < minm) then minm := a;
  end;

  if (minn = minm) then s := minn
  else if (minn < minm) then s := minn * 10 + minm
  else if (minn > minm) then s := minm * 10 + minn;

  writeln(s);
end.

type
  mass = array [1..10000] of integer;
function min(b : integer; a : mass) : integer;
var
  i, min1 : integer;
begin
  if b < 1 then
    exit;
  min1 := a[1];
  for i := 2 to b do
    if a[i] < min1 then
      min1 := a[i];
  min := min1;
end;
var
  a, b : mass;
  i, n, m : integer;
begin
  read(n, m);
  for i := 1 to n do
    read(a[i]);
  for i := 1 to m do
    read(b[i]);
  if min(n, a) = min(m, b)
    then
      writeln(min(n, a))
    else
      writeln(min(n, a), min(m, b));
end.

var a,b:array[1..1000] of longint;
    i,k,n,m,x,y:longint;
begin
readln(n,m);
for i:=1 to n do
 read(a[i]);
for i:=1 to m do
 read(b[i]);
for i:=1 to n-1 do
 for k:=i+1 to n do
  if a[i]>a[k]
  then begin
   x:=a[i];
   a[i]:=a[k];
   a[k]:=x;
  end;
for i:=1 to m-1 do
 for k:=i+1 to m do
  if b[i]>b[k]
  then begin
   x:=b[i];
   b[i]:=b[k];
   b[k]:=x;
  end;
x:=a[1];
y:=b[1];
if x=y
then writeln(x)
else if x>y
     then writeln(y,'',x)
     else writeln(x,'',y);
end.

program x1;
var q,n,m,min1,min2,i:integer;
begin
readln(n,m);
read(q);
min1:=q;
for i:=1 to n-1 do begin
read(q);
if q<min1 then min1:=q;
end;
read(q);
min2:=q;
for i:=1 to m-1 do begin
read(q);
if q<min2 then min2:=q;
end;
if min1=min2 then write(min1) else if min1>min2 then write(min2,min1) else write(min1,min2);
end.

var
n,m,i,j,min1,min2:integer;
a:array[1..100] of integer;
b:array[1..100] of integer;
begin
min1:=10;
min2:=10;
read(n);
read(m);
for i:=1 to n do
read(a[i]);
for j:=1 to m do
read(b[j]);
for i:=1 to n do
if (a[i]<min1) then min1:=a[i];
for j:=1 to m do
if (b[j]<min2) then min2:=b[j];
writeln(min1);
writeln(min2);
if (min1=min2) then write(min1)
else begin
if (min1<min2) then write(min1,min2)
else writeln(min2,min1);
end;
end.

program hm;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  n, m, min1, min2, i: integer;
  a, b: array[1..19] of integer;
begin
  readln(n, m);
  for i := 1 to n do
    read(a[i]);
  for i := 1 to m do
    read(b[i]);

  min1 := 10;
  min2 := 10;

  for i := 1 to n do
    if (min1 > a[i]) then min1 := a[i];

  for i := 1 to m do
    if (min2 > b[i]) then min2 := b[i];

  if (min1 = min2) then write(min1)
    else
  if (min1 > min2) then write(min2,min1)
    else
  write(min1,min2);
end.

Program P1;
var n,m,i,a,b,i2,e,e2: integer; a1,a2: array [0..1000] of integer;
begin
a:=9; b:=9; e:=10;
readln(n,m);
for i:= 1 to n do
read(a1[i]);

for i:= 1 to m do
read(a2[i]);

for i:= 1 to n do begin
if a1[i]<a then a:=a1[i];

for i2:= 1 to m do
if (a2[i2]=a1[i]) and (a2[i2]<e) then e:=a2[i2];
end;
if e=10 then begin
for i:= 1 to m do
if a2[i]<b then b:=a2[i];
end;
if e<10 then
write(e) else
write(a,b);
end.

var
        i,n,m,min2,min1,k:longint;
Begin
        readln(n,m);
        min1:=1000;
        min2:=1000;
        for i:=1 to n do
        begin
                read(k);
                if k<min1 then min1:=k;
        end;
        for i:=1 to m do
        begin
                read(k);
                if k<min2 then min2:=k;
        end;
        if min1>min2 then writeln(min2*10+min1) else
        if min2>min1 then writeln(min1*10+min2) else
        writeln(min1);
End.

var a:array[1..100] of integer;
    b:array[1..100] of integer;
    i,x1,x2,min1,min2,n,m:integer;
Begin
  read(n,m);
  min1:=30000;
  min2:=30000;
  for i:=1 to n do read(a[i]);
  for i:=1 to m do read(b[i]);
  for i:=1 to n do if a[i]<min1 then min1:=a[i];
  for i:=1 to m do if b[i]<min2 then min2:=b[i];
  if min1=min2 then write(min1)
               else if min1<min2 then write(min1,min2)
                                 else write(min2,min1)
end.

{$APPTYPE CONSOLE}
var     i,n,m,a,min1,min2: 1..9;
begin
  read(n,m);
  read(min1);
  if n>1 then begin
  for i:=1 to n-1 do begin
   read(a);
   if a<min1 then min1:=a;
  end;
  end;
  read(min2);
  if m>1 then begin
  for i:=1 to m-1 do begin
   read(a);
   if a<min2 then min2:=a;
  end;
  end;
  if min1=min2 then write(min1);
  if min1>min2 then write((min2*10)+min1);
  if min1<min2 then write((min1*10)+min2);

end.

var
  n, m, min1, min2, i, x: byte;

begin
  read(n, m);
  read(min1);
  i := 2;
  while i <= n do
  begin
    read(x);
    if x < min1 then min1 := x;
    i := i + 1;
  end;
  read(min2);
  i := 2;
  while i <= m do
  begin
    read(x);
    if x < min2 then min2 := x;
    i := i + 1;
  end;
  if min1 < min2 then write(min1, min2)
  else if min2 < min1 then write(min2, min1)
  else write(min1);
end.

var a, b, c, d, n, m:integer;
begin
c:=9;
d:=9;
read(n, m);
while (n<>0) do
begin
read(a);
n:= n-1;
if (a<c) then
c:=a;
end;

while (m<>0) do
begin
read(b);
m:= m-1;
if (b<d) then
d:=b;
end;
if (d <> c) then
begin
if (d>c) then
writeln(c*10 + d)
else
writeln(d*10 + c);
end
else
writeln(c);
end.

program hm;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  n, m, min1, min2, i: integer;
  a, b: array[1..19] of integer;
begin
  readln(n, m);
  for i := 1 to n do
    read(a[i]);
  for i := 1 to m do
    read(b[i]);

  min1 := a[1];
  min2 := b[1];

  for i := 2 to n do
    if (min1 > a[i]) then min1 := a[i];

  for i := 2 to m do
    if (min2 > b[i]) then min2 := b[i];

  if (min1 = min2) then write(min1)
    else
  if (min1 > min2) then write(min2,min1)
    else
  write(min1,min2);
end.

Program P1;
var n,m,i,a,b,i2,e,e2: integer; a1,a2: array [0..1000] of integer;
begin
a:=9; b:=9; e:=9;
readln(n,m);
for i:= 1 to n do
read(a1[i]);

for i:= 1 to m do
read(a2[i]);

for i:= 1 to n do begin
if a1[i]<a then a:=a1[i];

for i2:= 1 to m do
if (a2[i2]=a1[i]) and (a2[i2]<e) then e:=a2[i2];
end;
if e=9 then begin
for i:= 1 to m do
if a2[i]<b then b:=a2[i];
end;
if e<9 then
write(e) else
write(a,b);
end.

type
  mass = array [1..10000] of integer;
function min1(a : integer; b : integer) : integer;
var
  l, r : integer;
begin
  if l > r
    then
      min1 := r
    else
      min1 := l;
end;
function min(b : integer; a : mass) : integer;
var
  i, min1 : integer;
begin
  if b < 1 then
    exit;
  min1 := a[1];
  for i := 2 to b do
    if a[i] < min1 then
      min1 := a[i];
  min := min1;
end;
var
  a, b : mass;
  i, n, m : integer;
begin
  read(n, m);
  for i := 1 to n do
    read(a[i]);
  for i := 1 to m do
    read(b[i]);
  if (n = 1) and (m = 1)
    then
      begin
        writeln(b[1] * 10 + a[1]);
        exit;
      end;
  if min(n, a) = min(m, b)
    then
      writeln(min(n, a))
    else
      writeln(min(n, a), min(m, b));
end.

var
min,ci,i,j,ak,bk : int64;
a,b : array[1..9] of byte;
c : array[1..171] of byte;
begin
  ci:=0;
  min:=100;
  read(ak,bk);
  if (ak<1) or (ak>9) or (bk<1) or (bk>9) then exit;
  for i:=1 to ak do
  begin
     read(a[i]);
     if (a[i]<1) or (a[i]>9) then exit;
  end;
  for j:=1 to bk do
  begin
     read(b[j]);
     if (a[i]<1) or (a[i]>9) then exit;
  end;

  for i:=1 to ak do
     for j:=1 to bk do
     begin
       inc(ci);
       c[ci]:=10*a[i]+b[j];
     end;
  for i:=1 to ak do
     for j:=1 to bk do
     begin
       inc(ci);
       c[ci]:=10*b[j]+a[i];
     end;
  for i:=1 to ak do
     for j:=1 to bk do
        if a[i]=b[j] then
        begin
          inc(ci);
          c[ci]:=a[i];
        end;

  for i:=1 to ci do
     if c[i]<min then min:=c[i];

  for i:=1 to ci do
     write(c[i],' ');
  writeln;
  write('min=',min);
end.

Var
k,i,k1,l,l1,n,m:integer;
Begin
read(n,m);
read(k);
k1:=k;
for i:=1 to n-1 do
 begin
 read(k);
 if k1>k then k1:=k;
 end;
read(l);
l1:=l;
for i:=1 to m-1 do
 begin
 read(l);
 if l1>l then l1:=l;
 end;
if l1>k1 then write(k1*10+l1) else
 if l1<k1 then write(l1*10+k1) else write(k1);
end.

var a,b:array[1..9] of longint;i,k,x,min1,min2,n1,n2:longint;
begin
read(n1,n2);
for i:=1 to n1 do
 read(a[i]);
for i:=1 to n2 do
 read(b[i]);
for i:=1 to n1 do
 for k:=i+1 to n1 do
  if a[i]>a[k]
  then
   begin
    x:=a[i];
    a[i]:=a[k];
    a[k]:=x;
   end;
for i:=1 to n2 do
 for k:=i+1 to n2 do
  if b[i]>b[k]
  then
   begin
    x:=b[i];
    b[i]:=b[k];
    b[k]:=x;
   end;
min1:=a[1];
min2:=b[1];
if min1=min2
then writeln(min1)
else
 if min1>min2
 then writeln(min2,min1)
 else
  if min1<min2
  then writeln(min1,min2);
end.

var a:array[1..100] of integer;
    b:array[1..100] of integer;
    i,x1,x2,min1,min2,n,m:integer;
Begin
  read(n,m);
  min1:=30000;
  min2:=30000;
  for i:=1 to n do read(a[i]);
  for i:=1 to m do read(b[i]);
  for i:=1 to n do if a[i]<min1 then min1:=a[i];
  for i:=1 to m do if b[i]<min2 then min2:=b[i];
  if min1=min2 then write(min1)
               else if min1<min2 then write(min1*10+min2)
                                 else write(min2*10+min1)
end.

var
n,m,i,j,min1,min2:integer;
a:array[1..100] of integer;
b:array[1..100] of integer;
begin
min1:=10;
min2:=10;
read(n);
read(m);
for i:=1 to n do
read(a[i]);
for j:=1 to m do
read(b[j]);
for i:=1 to n do
if (a[i]<min1) then min1:=a[i];
for j:=1 to n do
if (b[j]<min2) then min2:=b[j];
if (min1<min2) then write(min1,min2)
else writeln(min2,min1);
end.

var min2,min,n,m,i:integer;
a:array[1..10] of integer;
b:array[1..10]  of integer;
begin
read(n);
read(m);
for i:=1 to n do
  read(a[i]);
for i:=1 to m do
  read(b[i]);
min:=a[1];
min2:=b[1];
for i:=1 to n do
    if a[i]<min then min:=a[i];
for i:=1 to m do
    if b[i]<min2 then min2:=b[i];
if min=min2 then writeln(min)
  else
    begin
      if min>min2 then
        begin
          min:=min2-min;
          min2:=min2-min;
          min:=min2+min;
        end;
          writeln(min*10+min2);
    end;
end.

var
n,m,i,j,min1,min2:integer;
a:array[1..100] of integer;
b:array[1..100] of integer;
begin
min1:=10;
min2:=10;
read(n);
read(m);
for i:=1 to n do
read(a[i]);
for j:=1 to m do
read(b[j]);
for i:=1 to n do
if (a[i]<min1) then min1:=a[i];
for j:=1 to m do
if (b[j]<min2) then min2:=b[j];
if (min1=min2) then write(min1)
else begin
if (min1<min2) then write(min1*10+min2)
else writeln(min2*10+min1);
end;
end.

var a,b:array[1..9] of longint;l1,l2,n,m,i,j,min1,min2,o,x,k,d:longint;s1,s2,s3:string;
begin
read(n,m);
for i:=1 to n do
 read(a[i]);
for i:=1 to m do
 read(b[i]);
for i:=1 to n do
 for k:=i+1 to n do
  if a[i]>a[k]
  then
   begin
    x:=a[i];
    a[i]:=a[k];
    a[k]:=x;
   end;
for i:=1 to m do
 for k:=i+1 to m do
  if b[i]>b[k]
  then
   begin
    x:=b[i];
    b[i]:=b[k];
    b[k]:=x;
   end;
min1:=a[1];
min2:=b[1];
for i:=1 to n do
 begin
 for j:=1 to m do
  if a[i]=b[j]
  then
    begin
     o:=a[i];
     inc(d);
     break;
    end;
 if d>0
 then break;
end;
str(min1,s1);
str(min2,s2);
l1:=length(s1);
l2:=length(s2);
s3:=copy(s1,1,l1)+copy(s2,1,l2);
val(s3,x);
if (x<o) or (o=0)
then writeln(x)
else writeln(o);
end.

var
n,m,min1,min2,a,b,i:integer;
Begin
  read(n,m);
  min1:=20;
  min2:=20;
  for i:=1 to n do
  begin
    read(a);
    if a<min1 then min1:=a;
  end;
  for i:=1 to m do
  begin
    read(b);
    if b<min2 then min2:=b;
  end;
  if min1=min2 then  writeln(min1)
  else
      if (min2 > min1) then
         writeln(min1*10+min2)
      else
         writeln(min2*10+min1);
End.

program cras;
var n,m,x,i: integer;
    a,b: array [1..9] of integer;
begin
  for i:=1 to 9 do
    begin
      a[i]:=0;
      b[i]:=0;
    end;
  read(n,m);
  for i:=1 to n do
    begin
      read(x);
      a[x]:=1;
    end;
  for i:=1 to m do
    begin
      read(x);
      b[x]:=1;
    end;
  i:=1;
  while(a[i]=0) do i:=i+1;
  x:=1;
  while(b[x]=0) do x:=x+1;
  if x>i then x:=i*10+x
  else if i>x then x:=x*10+i;
  for i:=1 to 9 do
    if a[i]=b[i] then
      if x>i then
        begin
          x:=i;
          break;
        end;
  writeln(x);
end.

Program lol;
var n1,n2,a,b,min,i:integer; s1,s2,n:string;
begin
readln(n);
val(copy(n,1,1),n1,a);
val(copy(n,3,1),n2,a);
readln(s1);
readln(s2);
min:=10;
for i:=1 to n1 do
begin
  val(copy(s1,i*2-1,1),a,b);
  if a<min then min:=a;
end;
a:=min;
min:=10;
i:=1;
while i<=n2  do
begin
  val(copy(s2,i*2-1,1),b,n1);
  if b<min then min:=b;
  if b=a then
  begin
  min:=a;
  i:=n2
  end;
  i:=i+1;
end;
b:=min;
if a<b then writeln(a*10+b);
if a=b then writeln(a);
if a>b then writeln(b*10+a)
end.

var n,m,x,i,j:longint;
f1,f2:array[0..10]of boolean;
begin
  readln(n,m);
  for i:=1 to n do
    begin
	  read(x);
	  f1[x]:=true;
	end;
  for i:=1 to m do
    begin
	  read(x);
	  f2[x]:=true;
	end;
  for i:=1 to 9 do
    begin
	  if (f1[i]=true)and(f2[i]=true) then begin writeln(i); exit; end
	end;
  for i:=1 to 9 do
    if f1[i]=true then break;
  for j:=1 to 9 do
    if f2[j]=true then break;
  writeln(i,j);
end.

var a,b,i,c,min,j:shortint;
    MLP,FIM: array [1..10] of shortint;
begin
  read(a,b);
  for i:=1 to a do read(MLP[i]);
  for i:=1 to b do read(FIM[i]);
  for i:=1 to a-1 do
    for j:=1 to a-i do
      if (MLP[j+1]<MLP[j]) then
        begin
        c:=MLP[j+1];
        MLP[j+1]:=MLP[j];
        MLP[j]:=c;
        end;
  for i:=1 to b-1 do
    for j:=1 to b-i do
      if (FIM[j+1]<FIM[j]) then
        begin
        c:=FIM[j+1];
        FIM[j+1]:=FIM[j];
        FIM[j]:=c;
        end;
  c:=99;
  for i:=1 to a do
    for j:=1 to b do
      if (MLP[i]=FIM[j]) and (c=99) then
        begin
        c:=98;
        write(MLP[i]);
        end;
  if (c=99) then
    begin
    for i:=1 to a do
      if (MLP[i]<c) then c:=MLP[i];
    a:=c;
    c:=99;
    for i:=1 to a do
      if (FIM[i]<c) then c:=FIM[i];
    if (c<a) then write(c,a)
    else write(a,c)
    end;
end.

var
  n, m, i, j, min1, min2, j1, same: integer;
  a, b: array [1..9] of integer;

begin
  same := 0;
  min1 := 10;
  min2 := 10;
  read(n, m);
  for i := 1 to n do
  begin
    read(a[i]);
    if a[i] < min1 then min1 := a[i];
  end;
  for j := 1 to m do
  begin
    read(b[j]);
    if b[j] < min2 then min2 := b[j];
    for j1 := 1 to n do
      if a[j1] = b[j] then same := b[j];
  end;

  if same <> 0 then writeln(b[j])
  else if min1 < min2 then writeln(min1, min2)
  else writeln(min2, min1);

end.

var
s,c:string;
a,b:array[1..10]of integer;
n,m,i,mina,minb,j,q,f:integer;
begin
read(n,m);
mina:=9;
minb:=9;
for i:= 1 to n do begin
read(a[i]);
if a[i]<mina then mina:=a[i];
end;
for i:= 1 to m do begin
read(b[i]);
if b[i]<minb then minb:=b[i];
end;
q:=9;
f:=0;
for i:= 1 to n do
 for j:= 1 to n do
 if (a[i]=b[j])and(a[i]<=q) then begin q:=a[i];f:=1;end;
if f=1 then begin writeln(q);exit;end;
if mina=minb then begin writeln(mina);exit;end;
if mina>minb then begin str(mina,s);str(minb,c);writeln(c+s);exit;end;
if mina<minb then begin str(mina,s);str(minb,c);writeln(s+c);exit;end;
end.

var n,m,ch,i,min1,min2,j:integer; a,b:array[1..10] of word;
label 1;
begin
readln(n,m);
For i:=1 to n-1 do
  read(a[i]);
readln(a[n]);
For i:=1 to m do
   read(b[i]);
min1:=a[1];
min2:=b[1];
For i:=1 to n do
  if a[i]<min1
    then min1:=a[i];
For i:=1 to m do
  if b[i]<min2
    then min2:=b[i];
 if min1=min2
   then begin write(min2); goto 1; end;
For i:=2 to n do
  begin
  For j:=2 to m do
    if a[i]=b[j]
      then begin write(a[i]); goto 1; end

  end;

if min1=min2
  then begin write(min1); goto 1; end;
if min1>min2
  then ch:=min2*10+min1
  else ch:=min1*10+min2;
write(ch);
1:end.