#include <cstdio>
#include <cctype>
#include <set>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
typedef pair<string, int> PSI;

char s[256];

void ParseString(string s, int k, set<PSI> &h) {
	int i;
	char c;
	string cur="";
	for (i=0; i<s.length(); i++) {
		c = s[i];
		if (c>='a' && c<='z')
			c = toupper(c);
		if (c>='A' && c<='Z')
			cur += c;
		else if (cur.length()>0) {
			h.insert(PSI(cur,k));
			cur = "";
		}
	}
	if (cur.length()>0)
		h.insert(PSI(cur,k));
}

int main() {
	int i,n,m,p,q;
	freopen("document.in", "rt", stdin);
	freopen("document.out", "wt", stdout);
	gets(s);
	sscanf(s, "%d", &n);
	vector<string> v;

	for (; gets(s); )
		v.push_back((string)s);
	m = v.size();

	vector<bool> el(m);
	vector<int> par(m),line(m),page(m),lineInPar(m);

	for (i=0,p=1,q=1; i<m; i++) {
		el[i] = (v[i].length()==0);
		par[i] = p;
		line[i] = q;
		if (el[i]) {
			lineInPar[p] = q-1;
			p++;
			q = 0;
		}
		q++;
	}
	lineInPar[p] = q-1;

	for (i=q=0,p=1; i<m; i++) {
		page[i] = p;
		if (q>0 || !el[i]) q++;
		if (q==n) {
			p++;
			q = 0;
			if (line[i]==1 && lineInPar[par[i]]>1) {
				page[i] = p;
				q = 1;
			}
			else if (line[i]+1 == lineInPar[par[i]]) {
				if (lineInPar[par[i]]==3) {
					page[i-1] = p;
					page[i] = p;
					q = 2;
				}
				else {
					page[i] = p;
					q = 1;
				}
			}
		}
	}

	set<PSI> h;
	for (i=0; i<m; i++)
		ParseString(v[i], page[i], h);

	set<PSI>::iterator it;
	if (h.size()==0) return 0;
	string cur = (h.begin())->first;
	string next;
	int l = (h.begin())->second;
	int r = l;
	bool first = true;

	printf("%s ", cur.c_str());
	it=h.begin();
	it++;
	for (; it!=h.end(); it++) {
		next = it->first.c_str();
		p = it->second;
		if (cur==next) {
			if (p==r+1)
				r++;
			else {
				if (first)
					first = false;
				else
					printf(",");
				if (l==r)
					printf("%d", l);
				else if (l+1==r)
					printf("%d,%d", l, r);
				else
					printf("%d-%d", l, r);
				l = r = p;
			}
		}
		else {
			if (first)
				first = false;
			else
				printf(",");
			if (l==r)
				printf("%d", l);
			else if (l+1==r)
				printf("%d,%d", l, r);
			else
				printf("%d-%d", l, r);
			l = r = p;

			cur = next;
			printf("\n%s ", cur.c_str());
			first = true;
		}
		//printf("%s %d\n", it->first.c_str(), it->second);
	}

	if (first)
		first = false;
	else
		printf(",");
	if (l==r)
		printf("%d", l);
	else if (l+1==r)
		printf("%d,%d", l, r);
	else
		printf("%d-%d", l, r);
	printf("\n");
/*
	printf("%d\n", m);
	for (i=0; i<m; i++) {
		printf("%d %d %d %d\n", el[i], par[i], line[i], page[i]);
	}
*/

	return 0;
}