program shhv;
const
    nm = 50;
var
    a: array[1..nm] of byte;
    t: array[1..nm] of boolean;
    f: array[1..nm] of longword;
    i, j, n: byte;
    s: longword;
begin
    n := 0;
    while not seekeoln do
        begin
            inc(n);
            read(a[n]);
        end;
    fillchar(t, sizeof(t), true);
    s := 1; f[0] := 1;
    for i := 1 to n do f[i] := f[i-1] * i;
    for i := 1 to n do
        begin
            for j := 1 to a[i]-1 do
                if t[j] then s := s + f[n-i];
            t[a[i]] := false;
        end;
    writeln(s);
    readln(s);
    fillchar(t, sizeof(t), true);
    for i := 1 to n do
        begin
            for j := 1 to n do
                if t[j] then
                    if (s > f[n-i]) then s := s - f[n-i] else break;
            a[i] := j;
            t[j] := false;
        end;
    for i := 1 to n do write(a[i], ' ');
end.



