var n,i,m,pp,a:integer;
Begin
pp:=0;
m:=0;
Readln(n);
for i:=1 to n do Begin
Readln(a);
if (pp=0) and (a mod 3=0) then Begin
pp:=1;
m:=a;
end;
if (pp=1) and (a mod 3=0) and (a<m) then m:=a;
Writeln(m);
end.
var n,i,m,pp,a:integer;
Begin
pp:=0;
m:=0;
Readln(n);
for i:=1 to n do Begin
Readln(a);
if (pp=0) and (a mod 3=0) then Begin
pp:=1;
m:=a;
end;
if (pp=1) and (a mod 3=0) and (a<m) then m:=a;
end;
Writeln(m);
end.