#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string>
using namespace std;
typedef struct
{
string ch;
long int cnt;
}DNA;
int check(string sh)
{
int num;
int k=0;
num=sh.length();
for(int j=0;j<num;j++)
for(int o=j+1;o<num;o++)
{
if(sh[j]>sh[o])
k++;
}
return k;
}
int main()
{
int n,m;
cin>>n>>m;
int i=0;
int y;
string h;
DNA str[102];
while(i<m)
{
i++;
cin>>str[i].ch;
str[i].cnt = 0;
str[i].cnt=check(str[i].ch);
}
for(int j=0;j<m;j++)
for(int o=j+1;o<m;o++)
{
if(str[j].cnt>str[o].cnt)
{
h=str[o].ch;
str[o].ch=str[j].ch;
str[j].ch=h;
y=str[o].cnt;
str[o].cnt=str[j].cnt;
str[j].cnt=y;
}
}
for(int j=0;j<m;j++)
{
cout<<str[j].ch<<endl;
}
return 0;
}
#include<stdlib.h>
#include<iostream>
#include<string>
using namespace std;
typedef struct
{
string ch;
long int cnt;
}DNA;
int check(string sh)
{
int num;
int k=0;
num=sh.length();
for(int j=0;j<num;j++)
for(int o=j+1;o<num;o++)
{
if(sh[j]>sh[o])
k++;
}
return k;
}
int main()
{
int n,m;
cin>>n>>m;
int i=0;
int y;
string h;
DNA str[102];
while(i<m)
{
i++;
cin>>str[i].ch;
str[i].cnt = 0;
str[i].cnt=check(str[i].ch);
}
for(int j=0;j<m;j++)
for(int o=j+1;o<m;o++)
{
if(str[j].cnt>str[o].cnt)
{
h=str[o].ch;
str[o].ch=str[j].ch;
str[j].ch=h;
y=str[o].cnt;
str[o].cnt=str[j].cnt;
str[j].cnt=y;
}
}
for(int j=0;j<m;j++)
{
cout<<str[j].ch<<endl;
}
return 0;
}