#!/usr/bin/perl -w

use strict;

my %count;
# column to group by (1,...)
my $cols = shift @ARGV;
# columns to show (1,2,...) -- shows LAST occurance if not same as $cols
my $cols_show = shift @ARGV;
my %show;
my @line;

while(<STDIN>) {
	chomp;
	@line = split(/\s+/,$_);
	if ($cols) {

		sub col_key {
			my $range = shift || return;

			my $k;

			sub add2key {
				my $i = shift || return;
				return unless $i =~ m/^\d+/;
				$i--;
				return if ($i > ($#line + 1));
				#print "# a2k: $i => $line[$i] [",join(" ",@line),"]\n";
				return $line[$i] . " ";
			}

			if ($range =~ m/^(\d+)-$/) {
				$k .= add2key($_) foreach ( $1 .. ($#line + 1) );
			} elsif ($range =~ m/^-(\d+)$/) {
				$k .= add2key($_) foreach ( 0 .. $1 );
			} elsif ($range =~ m/^(\d+)-(\d+)$/) {
				$k .= add2key($_) foreach ( $1 .. $2 );
			} else {
				$k .= add2key($range);
			}
			return $k || 'null';
		}

		my $key;

		if ($cols =~ m/,/) {
			foreach my $c (split(/,/,$cols)) {
				$key .= col_key($c);
			}
		} else {
			$key = col_key($cols);
		}

		$key =~ s/\s+$//;
		$count{$key}++;
		#print "# $key => $count{$key}\n";
	} else {
		# group by add whitespace separated data
		foreach my $c (@line) {
			$count{$c}++;
		}
	}
}

foreach my $c (sort keys %count) {
	print $count{$c}," $c\n";
}
