var
s : string;
c : char;
i, j, l : integer;
begin
assign(input,'input.txt'); reset(input);
assign(output,'output.txt'); rewrite(output);
readln(s);
l:=length(s); i:=l-1;
while (i>0) and (s[i]>=s[i+1]) do i:=i-1;
if i=0 then write(0) else
begin
j:=l;
while s[i]>=s[j] do j:=j-1;
c:=s[i]; s[i]:=s[j]; s[j]:=c;
for j:=1 to (l-i) div 2 do
begin
c:=s[i+j]; s[i+j]:=s[l+1-j]; s[l+1-j]:=c
end;
write(s)
end
end.
s : string;
c : char;
i, j, l : integer;
begin
assign(input,'input.txt'); reset(input);
assign(output,'output.txt'); rewrite(output);
readln(s);
l:=length(s); i:=l-1;
while (i>0) and (s[i]>=s[i+1]) do i:=i-1;
if i=0 then write(0) else
begin
j:=l;
while s[i]>=s[j] do j:=j-1;
c:=s[i]; s[i]:=s[j]; s[j]:=c;
for j:=1 to (l-i) div 2 do
begin
c:=s[i+j]; s[i+j]:=s[l+1-j]; s[l+1-j]:=c
end;
write(s)
end
end.