import java.util.Scanner;
import java.util.Arrays;
import java.io.File;
import java.io.PrintWriter;

/**
 * Created by IntelliJ IDEA.
 * User: o_215
 * Date: 18.11.2009
 * Time: 18:53:42
 * To change this template use File | Settings | File Templates.
 */
public class D {
    static class Pair implements Comparable<Pair> {
        String value; int brokes;
        public Pair(String s){value=s;brokes=getBrokes(s);}
        public int getBrokes(String s){
            int res=0;
            for (int i=0;i<s.length();i++){
                for(int j=i;j<s.length();j++){
                    if (s.charAt(i)>s.charAt(j)){res++;}
                }
            }
        return res;
        }
        public int compareTo(Pair p) {
            return brokes-p.brokes;
        }
    }

    public static void main(String[] args) throws Exception{
        Scanner in=new Scanner(new File("sorting.in"));
        PrintWriter out=new PrintWriter(new File("sorting.out"));
        int n=in.nextInt();
        int m=in.nextInt();in.nextLine();
        Pair[] p=new Pair[m];
        for (int i=0;i<m;i++){
            p[i]=new Pair(in.nextLine());
        }
        Arrays.sort(p);
        for(int i=0;i<m;i++){
            out.println(p[i].value);
        }
        in.close();
        out.close();
    }

}

