import java.util.*;
import java.io.*;

public class Anagram {
    public static void main(String[] args) {
        int minGroupSize = Integer.parseInt(args[1]);

        //Read words from file and put into a simulated multimap
        Map<String, Set<String>> m = new HashMap<String, Set<String>>();
       
        try {
            //peut lever FileNotFoundException
            Scanner s = new Scanner(new File(args[0]));
            String word;
            while(s.hasNext()) {
                word = s.next();
                String alpha = alphabetize(word);
                Set<String> l = m.get(alpha);
                if (l == null) {
		    l = new HashSet<String>();
                    m.put(alpha, l);
		}
                l.add(word);
            }
            s.close(); // ne lève pas d'exception
        } catch(IOException e) {
            System.err.println(e);
            System.exit(1);
        }

        //Print all permutation groups above size threshold
        for (Set<String> l : m.values()) {
            if (l.size() >= minGroupSize)
                System.out.println(l.size() + ": " + l);
        }
        System.out.println(m);
    }

    private static String alphabetize(String s) {
        char[] tab = s.toCharArray();
        Arrays.sort(tab);  
        return new String(tab);
    }
}
