{
TASK: SLIDESV
LANG: PASCAL
}
program slides1;

  const PRF = 'SLIDES.DAT';
        RZF = 'SLIDES.REZ';
        MAX_M = 2000;
        MAX_N = 1000;

  type Tmas = array [0..MAX_M] of longint;

       Tlentl = array [0..MAX_M, 0..MAX_N] of boolean;
       Tlenti = array [0..MAX_M, 0..MAX_N] of longint;

  var N, M: longint;
      ilg: Tmas;
      nr:  Tmas;

      l: Tlenti;
      imta: Tlentl;
      ats: Tmas;

  procedure Qsort (ind1, ind2: longint);
    var y, i, j, tarp: longint;
  begin
    i := ind1; j := ind2;
    y := ilg[ind1];
    repeat
      while y > ilg[i] do i := i + 1;
      while y < ilg[j] do j := j - 1;
      if i <= j
         then begin
                tarp := ilg[i]; ilg[i] := ilg[j]; ilg[j] := tarp;
                tarp := nr[i]; nr[i] := nr[j]; nr[j] := tarp;
                i := i + 1;
                j := j - 1;
              end;
    until i > j;
    if ind1 < j then Qsort (ind1, j);
    if i < ind2 then Qsort (i, ind2);
  end; { Qsort }

  procedure dinam (var l: Tlenti; var imta: Tlentl);
    var s, v: longint;
  begin
    for s := 0 to M do
      for v := 0 to N do
         if ((s=0) or (v=0)) or (2*v > s) then l[s, v] := 0;
    for v := 1 to N do { vaiku }
      begin
        { atveji kai s = 2*v }
        s := 2*v;
        l[s, v] := abs (ilg[s]-ilg[s-1]) + l[s-2, v-1];
        imta[s, v] := true;
        { like atvejai }
        for s := 2*v+1 to M do { slidziu }
          begin
            if abs (ilg[s]-ilg[s-1]) + l[s-2, v-1] < l[s-1, v]
               then begin
                      l[s, v] := abs (ilg[s]-ilg[s-1]) + l[s-2, v-1];
                      imta[s, v] := true;
                    end
               else begin
                      l[s, v] := l[s-1, v];
                      imta[s, v] := false;
                    end;
         end;
      end;
   end; { dinamin }

  function min_suma: longint;
    var i, s, v: longint;
  begin
    min_suma := l[M, N];
    i := 0; s := M; v := N;
    while i < 2*N do
      if imta[s, v] { bûtinai imamos dvi gretimos slides }
         then begin
                i := i + 1;
                ats[i] := nr[s];
                i := i + 1;
                ats[i] := nr[s-1];
                v := v - 1;
                s := s - 2;
              end
         else s := s - 1;
  end;

  procedure skaityti (var N, M: longint; var ilg: Tmas);
    var f: text;
        i: longint;
  begin
    assign (f, PRF);
    reset (f);
    readln (f, N, M);
    for i := 1 to M do
      begin
        readln (f, ilg[i]);
        nr[i] := i;
      end;
    close (f);
  end; { skaityti }

  procedure rasyti (suma: longint);
    var f: text;
        i: longint;
  begin
    assign (f, RZF);
    rewrite (f);
    writeln (f, suma);
    for i := 1 to N do
      writeln (f, ats[2*i], ' ', ats[2*i-1]);
    close (f);
  end; { rasyti }

  var suma, s, v: longint;
begin
  skaityti (N, M, ilg);
  Qsort (1, M);
  dinam (l, imta);
  suma := min_suma;
  rasyti (min_suma);
end.
