program bojenje;
var i,j,t1,t2,n,m,x,Z,w: longint;
a: array[1..100000, 1..10000] of boolean;
d: array[1..10000] of boolean;
l: array[1..10000] of boolean;
begin
        read(n,m);
        writeln;
        read(x);
        if x=m then
             for i:=1 to m do
              a[1,i]:= false
        else if x>m-x then
                for i:=1 to m do
                        if i<=x then
                                a[1,i]:=false
                        else a[1,i]:=true
                else
                        for i:=1 to m do
                         if i<=m-x then
                                a[1,i]:=true
                        else a[1,i]:=false;
        for i:=2 to n do
        begin
                t1:=0;
                t2:=0;
                read(x);
                if x=m then
                         for j:=1 to m do
                          a[i,j]:= false
                else
                begin
                 for j:=1 to m do
                        if j<=x then
                        begin
                                d[j]:=false;
                                l[j]:=true;
                        end
                        else
                        begin
                              d[j]:=true;
                              l[j]:=false;
                        end;
                 for j:=1 to m do
                 begin
                        if a[i-1,j] xor d[j]=false
                                then t1:=t1+1;
                        if a[i-1,j] xor l[j]=false
                                then t2:=t2+1;
                 end;
                 if t1>t2 then
                        for j:=1 to m do
                                a[i,j]:=d[j]
                 else
                        for j:=1 to m do
                                a[i,j]:=l[j];
                 end;
        end;
        Z:=0;
        for j:=1 to m do
        begin
                t1:=0;
                t2:=0;
                for i:=1 to n do
                if a[i,j]=true
                 then t1:=t1+1
                   else t2:=t2+1;
                Z:=Z+(t1*t2);
        end;
        writeln;
        write(Z);
end.