{$I-,S-,Q-,R-,D-,L-,Y-,P-}
const
  maxS = 20000;
  maxp=20000;
  chars = 'ABCDEFGHIJKLMNOPQRSTUVWZYZ';

var
  w : array [1..maxS] of string;
  s : array [1..maxS] of string;
  size, cw:longint;
  
  cs:array[1..maxp]of string;
  pi,ci:array[1..maxp]of longint;

  was:array[1..maxp]of boolean;
  temps, word : string;
  sm, i, k, j, n, cnt, cur, z : longint;
  c : array [1..maxS] of longint;
  p, st, en : array [1..maxS] of longint;

procedure add(const s:string; i:longint);
begin
  if s='' then
    exit;
  inc(cw);
  cs[cw]:=s;
  ci[cw]:=i;
end;

procedure swap(i, j : longint);
var k : longint;
begin
  k := pi[i];
  pi[i] :=  pi[j];
  pi[j] := k;
end;

function more(i, j : longint) : boolean;
begin
  more := false;
  if (cs[pi[i]] > cs[pi[j]]) or ((cs[pi[i]] = cs[pi[j]]) and (ci[pi[i]] > ci[pi[j]])) then more := true;
end;

procedure heapify(i : longint);
var m : longint;
begin
  m := i;
  if 2*i <= size then
    if more(2*i, m) then m := 2*i;
  if 2*i+1 <= size then
    if more(2*i+1, m) then m := 2*i+1;
  if m <> i then
  begin
    swap(i, m);
    heapify(m);
  end;
end;

procedure hsort;
var i : longint;
begin
  size := cw;
  for i := cw downto 1 do
    heapify(i);
  for i := cw downto 2 do
  begin
    size := size - 1;
    swap(i, 1);
    heapify(1);
  end;
end;

procedure sort;
var i:longint;
begin
  for i:=1 to cw do
    pi[i]:=i;
  hsort;
end;

procedure outs(i:longint);
var st,en,j:longint;
    f:boolean;
begin
  write(cs[pi[i]],' ');
  st:=ci[pi[i]];
  en:=ci[pi[i]];
  j:=i+1;
  f:=true;
  while (j<=cw)and(cs[pi[i]]=cs[pi[j]]) do
    begin
      if ci[pi[j]]>en+1 then
        begin
          if not f then
            write(',');
          f:=false;
          if en>st+1 then
            write(st,'-',en)
          else
            if st=en then
              write(st)
            else
              write(st,',',en);
          st:=ci[pi[j]];
          en:=ci[pi[j]];
        end;
      en:=ci[pi[j]];
      inc(j);
    end;

          if not f then
            write(',');
          f:=false;
          if en>st+1 then
            write(st,'-',en)
          else
            if st=en then
              write(st)
            else
              write(st,',',en);
              
  writeln;
end;

procedure print;
var i,j:longint;
begin
  outs(1);
  j:=1;
  for i:=2 to cw do
    if cs[pi[i]]<>cs[pi[j]] then
      begin
        outs(i);
        j:=i;
      end;
end;

begin
  assign(input, 'document.in');
  reset(input);
  assign(output, 'document.out');
  rewrite(output);

  readln(n);
  cur := 1;
  i := 0;
  st[1] := 1;
  sm := 0;
  while not eof do
  begin
    inc(i);
    readln(s[i]);
    c[i] := cur;
    inc(sm);
    if s[i] = '' then 
    begin
      en[cur] := i - 1;
      inc(cur);
      st[cur] := i + 1;
      sm := 0;
    end;
  end;
  en[cur] := i;
  cnt := i;
  cur := 1;
  z := 1;
  for i := 1 to cnt do
  begin
    if (z = n) and (s[i] = '') then
    begin
      z := 1;
      inc(cur);
      p[i] := 0;
      continue;
    end;
    if (z = n) and (st[c[i]] = i) and (st[c[i]] <> en[c[i]]) then
    begin
      inc(cur);
      z := 2;
      p[i] := cur;
      continue;
    end;
    if (z = n) and (en[c[i]] = i + 1) and (en[c[i]] - st[c[i]] > 2) then
    begin
      inc(cur);
      z := 2;
      p[i] := cur;
      continue;
    end;
    if (z = n) and (en[c[i]] = i + 1) and (en[c[i]] = st[c[i]] + 1) then
    begin
      inc(cur);
      z := 2;
      p[i] := cur;
      continue;
    end;
    if (z = n) and (en[c[i]] = i + 1) and (en[c[i]] = st[c[i]] + 2) then
    begin
      inc(cur);
      z := 3;
      p[i] := cur;
      p[i - 1] := cur;
      continue;
    end;
    p[i] := cur;
    inc(z);
    if z > n then
    begin
      z := 1;
      inc(cur);
    end;
  end;

  cw:=0;
  for i := 1 to cnt do
  begin
    temps := s[i]+' ';
    for j := 1 to length(temps) do
      temps[j] := upcase(temps[j]);
    k := 1;
    word := '';
    while k <= length(temps) do
    begin
      if (pos(temps[k], chars) <> 0) then
        word := word + temps[k]
      else begin
        add(word, p[i]);
        word := '';
      end;
      inc(k);
    end;
  end;

  sort;
  fillchar(was,sizeof(was),false);
  print;

  close(input);
  close(output);
end.