#include<bits/stdc++.h>
using namespace std;
const int N =2e5+5;
const long long M = (long long)1e9+7;
typedef     long long       ll;
int Int() {
    int x;
    scanf("%d",&x);
    return x;
}
ll Long() {
    ll x;
    scanf("%I64d",&x);
    return x;
}
void Char(char* ch) {
    scanf("%s",ch);
}
template<typename t>t abs(t a) {
    if(a >= 0)return a;
    return -a ;
}
template<typename t>t Range(vector<t> v, t x) {
    ///Number of elements in v which is equal to x ;
    auto u = equal_range(v.begin(),v.end(),x);
    return u.second - u.first;
}
///error///
#define error(args...) {vector<string>_v=split(#args,',');err(_v.begin(),args);cout<<endl;}
vector<string> split(const string &s,char c) {
    vector<string>v;
    stringstream ss(s);
    string x;
    while(getline(ss,x,c))
        v.emplace_back(x);
    return move(v);
}

void err(vector<string>::iterator it) {}
template<typename T,typename... Args>
void err(vector<string>::iterator it,T a,Args...args) {
    cerr<<it->substr((*it)[0]==' ',it->length())<< " = " << a << " ";
    err(++it,args...);
}
///This DEBUGGER-tool need c++11///
#define     Int         Int()
#define     Long        Long()
#define     pii         pair<int,int>
#define     ff          first
#define     ss          second
#define     vi          vector<int>
#define     vl          vector<ll>
#define     pb          push_back
#define     all(x)      x.begin(),x.end()
#define     sq(a)       (a)*(a)

int a[N];
vector<int>res[N];
int index(vector<int>p, int v) {
    int l = 0 , r = (int)p.size() - 1 , mid = 0 ;
    while(l <= r) {
        int mid = (l + r) >> 1 ;
        if(p[mid] <= v) {
            r = mid - 1 ;
        } else {
            l = mid + 1 ;
        }
    }
    return l ;
}
int main() {
    //freopen("input.txt","r",stdin);
    int n = Int ;
    for(int i = 1 ; i <= n ; i++) {
        a[i] = Int ;
    }
    vector<int>pos ;
    int id = 0 ;
    for(int i = 1 ; i <= n ; i++) {
        if(i == 1) {
            pos.pb(a[i]);
            res[id].pb(a[i]);
        } else {
            int f = index(pos,a[i]);
            if(f == (int)pos.size()) {
                pos.pb(a[i]);
                res[++id].pb(a[i]);
            } else {
                pos[f] = a[i] ;
                res[f].pb(a[i]);
            }
        }
    }
   // cout<<(int)pos.size()<<endl;
    for(int i = 0 ; i <= id ; i++) {
        for(int j = 0 ; j < (int)res[i].size() ; j++) {
            printf("%d ",res[i][j]);
        }
        puts("");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N =2e5+5;
const long long M = (long long)1e9+7;
typedef     long long       ll;
int Int() {
    int x;
    scanf("%d",&x);
    return x;
}
ll Long() {
    ll x;
    scanf("%I64d",&x);
    return x;
}
void Char(char* ch) {
    scanf("%s",ch);
}
template<typename t>t abs(t a) {
    if(a >= 0)return a;
    return -a ;
}
template<typename t>t Range(vector<t> v, t x) {
    ///Number of elements in v which is equal to x ;
    auto u = equal_range(v.begin(),v.end(),x);
    return u.second - u.first;
}
///error///
#define error(args...) {vector<string>_v=split(#args,',');err(_v.begin(),args);cout<<endl;}
vector<string> split(const string &s,char c) {
    vector<string>v;
    stringstream ss(s);
    string x;
    while(getline(ss,x,c))
        v.emplace_back(x);
    return move(v);
}

void err(vector<string>::iterator it) {}
template<typename T,typename... Args>
void err(vector<string>::iterator it,T a,Args...args) {
    cerr<<it->substr((*it)[0]==' ',it->length())<< " = " << a << " ";
    err(++it,args...);
}
///This DEBUGGER-tool need c++11///
#define     Int         Int()
#define     Long        Long()
#define     pii         pair<int,int>
#define     ff          first
#define     ss          second
#define     vi          vector<int>
#define     vl          vector<ll>
#define     pb          push_back
#define     all(x)      x.begin(),x.end()
#define     sq(a)       (a)*(a)

int a[N];
vector<int>res[N];
int index(vector<int>p, int v) {
    int l = 0 , r = (int)p.size() - 1 , mid = 0 ;
    while(l <= r) {
        int mid = (l + r) >> 1 ;
        if(p[mid] <= v) {
            r = mid - 1 ;
        } else {
            l = mid + 1 ;
        }
    }
    return l ;
}
int main() {
    //freopen("input.txt","r",stdin);
    int n = Int ;
    for(int i = 1 ; i <= n ; i++) {
        a[i] = Int ;
    }
    vector<int>pos ;
    int id = 0 ;
    for(int i = 1 ; i <= n ; i++) {
        if(i == 1) {
            pos.pb(a[i]);
            res[id].pb(a[i]);
        } else {
            int f = index(pos,a[i] - 1);
            if(f == (int)pos.size()) {
                pos.pb(a[i]);
                res[++id].pb(a[i]);
            } else {
                pos[f] = a[i] ;
                res[f].pb(a[i]);
            }
        }
    }
   // cout<<(int)pos.size()<<endl;
    for(int i = 0 ; i <= id ; i++) {
        for(int j = 0 ; j < (int)res[i].size() ; j+h) {
            printf("%d ",res[i][j]);
        }
        puts("");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N =2e5+5;
const long long M = (long long)1e9+7;
typedef     long long       ll;
int Int() {
    int x;
    scanf("%d",&x);
    return x;
}
ll Long() {
    ll x;
    scanf("%I64d",&x);
    return x;
}
void Char(char* ch) {
    scanf("%s",ch);
}
template<typename t>t abs(t a) {
    if(a >= 0)return a;
    return -a ;
}
template<typename t>t Range(vector<t> v, t x) {
    ///Number of elements in v which is equal to x ;
    auto u = equal_range(v.begin(),v.end(),x);
    return u.second - u.first;
}
///error///
#define error(args...) {vector<string>_v=split(#args,',');err(_v.begin(),args);cout<<endl;}
vector<string> split(const string &s,char c) {
    vector<string>v;
    stringstream ss(s);
    string x;
    while(getline(ss,x,c))
        v.emplace_back(x);
    return move(v);
}

void err(vector<string>::iterator it) {}
template<typename T,typename... Args>
void err(vector<string>::iterator it,T a,Args...args) {
    cerr<<it->substr((*it)[0]==' ',it->length())<< " = " << a << " ";
    err(++it,args...);
}
///This DEBUGGER-tool need c++11///
#define     Int         Int()
#define     Long        Long()
#define     pii         pair<int,int>
#define     ff          first
#define     ss          second
#define     vi          vector<int>
#define     vl          vector<ll>
#define     pb          push_back
#define     all(x)      x.begin(),x.end()
#define     sq(a)       (a)*(a)

int a[N];
vector<int>res[N];
int index(vector<int>p, int v) {
    int l = 0 , r = (int)p.size() - 1 , mid = 0 ;
    while(l <= r) {
        int mid = (l + r) >> 1 ;
        if(p[mid] <= v) {
            r = mid - 1 ;
        } else {
            l = mid + 1 ;
        }
    }
    return l ;
}
int main() {
    //freopen("input.txt","r",stdin);
    int n = Int ;
    vector<int>pos ;
    int id = 0 ;
    for(int i = 1 ; i <= n ; i++) {
            a[i] = Int;
            if(i == 1) {
            pos.pb(a[i]);
            res[id].pb(a[i]);
        } else {
            int f = index(pos,a[i]);
            if(f<0) f = 0 ;
            if(f == (int)pos.size()) {
                pos.pb(a[i]);
                res[++id].pb(a[i]);
            } else {
                pos[f] = a[i] ;
                res[f].pb(a[i]);
            }
        }
    }
   // cout<<(int)pos.size()<<endl;
    for(int i = 0 ; i <= id ; i++) {
        for(int j = 0 ; j < (int)res[i].size() ; j++) {
            printf("%d ",res[i][j]);
        }
        puts("");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N =2e5+5;
const long long M = (long long)1e9+7;
typedef     long long       ll;
int Int() {
    int x;
    scanf("%d",&x);
    return x;
}
ll Long() {
    ll x;
    scanf("%I64d",&x);
    return x;
}
void Char(char* ch) {
    scanf("%s",ch);
}
template<typename t>t abs(t a) {
    if(a >= 0)return a;
    return -a ;
}
template<typename t>t Range(vector<t> v, t x) {
    ///Number of elements in v which is equal to x ;
    auto u = equal_range(v.begin(),v.end(),x);
    return u.second - u.first;
}
///error///
#define error(args...) {vector<string>_v=split(#args,',');err(_v.begin(),args);cout<<endl;}
vector<string> split(const string &s,char c) {
    vector<string>v;
    stringstream ss(s);
    string x;
    while(getline(ss,x,c))
        v.emplace_back(x);
    return move(v);
}

void err(vector<string>::iterator it) {}
template<typename T,typename... Args>
void err(vector<string>::iterator it,T a,Args...args) {
    cerr<<it->substr((*it)[0]==' ',it->length())<< " = " << a << " ";
    err(++it,args...);
}
///This DEBUGGER-tool need c++11///
#define     Int         Int()
#define     Long        Long()
#define     pii         pair<int,int>
#define     ff          first
#define     ss          second
#define     vi          vector<int>
#define     vl          vector<ll>
#define     pb          push_back
#define     all(x)      x.begin(),x.end()
#define     sq(a)       (a)*(a)

int a[N];
vector<int>res[N];
int index(vector<int>p, int v) {
    int l = 0 , r = (int)p.size() - 1 , mid = 0 ;
    while(l <= r) {
        int mid = (l + r) >> 1 ;
        if(p[mid] <= v) {
            r = mid - 1 ;
        } else {
            l = mid + 1 ;
        }
    }
    return l ;
}
int main() {
    //freopen("input.txt","r",stdin);
    int n = Int ;
    for(int i = 1 ; i <= n ; i++) {
        a[i] = Int ;
    }
    vector<int>pos ;
    int id = 0 ;
    for(int i = 1 ; i <= n ; i++) {
        if(i == 1) {
            pos.pb(a[i]);
            res[id].pb(a[i]);
        } else {
            int f = index(pos,a[i] - 1);
            if(f == (int)pos.size()) {
                pos.pb(a[i]);
                res[++id].pb(a[i]);
            } else {
                pos[f] = a[i] ;
                res[f].pb(a[i]);
            }
        }
    }
   // cout<<(int)pos.size()<<endl;
    for(int i = 0 ; i <= id ; i++) {
        for(int j = 0 ; j < (int)res[i].size() ; j++) {
            printf("%d ",res[i][j]);
        }
        puts("");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N =2e5+5;
const long long M = (long long)1e9+7;
typedef     long long       ll;
int Int() {
    int x;
    scanf("%d",&x);
    return x;
}
ll Long() {
    ll x;
    scanf("%I64d",&x);
    return x;
}
void Char(char* ch) {
    scanf("%s",ch);
}
template<typename t>t abs(t a) {
    if(a >= 0)return a;
    return -a ;
}
template<typename t>t Range(vector<t> v, t x) {
    ///Number of elements in v which is equal to x ;
    auto u = equal_range(v.begin(),v.end(),x);
    return u.second - u.first;
}
///error///
#define error(args...) {vector<string>_v=split(#args,',');err(_v.begin(),args);cout<<endl;}
vector<string> split(const string &s,char c) {
    vector<string>v;
    stringstream ss(s);
    string x;
    while(getline(ss,x,c))
        v.emplace_back(x);
    return move(v);
}

void err(vector<string>::iterator it) {}
template<typename T,typename... Args>
void err(vector<string>::iterator it,T a,Args...args) {
    cerr<<it->substr((*it)[0]==' ',it->length())<< " = " << a << " ";
    err(++it,args...);
}
///This DEBUGGER-tool need c++11///
#define     Int         Int()
#define     Long        Long()
#define     pii         pair<int,int>
#define     ff          first
#define     ss          second
#define     vi          vector<int>
#define     vl          vector<ll>
#define     pb          push_back
#define     all(x)      x.begin(),x.end()
#define     sq(a)       (a)*(a)

int a[N];
vector<int>res[N],pos;
int index(vector<int>p, int v) {
    int l = 0 , r = (int)p.size() - 1 , mid = 0 ;
    while(l <= r) {
        mid = (l + r) >> 1 ;
        if(p[mid] <= v) {
            r = mid - 1 ;
        } else {
            l = mid + 1 ;
        }
    }
    return l ;
}
int main() {
    //freopen("input.txt","r",stdin);
    int n = Int ;
  //  vector<int>pos ;
    int id = 0 ;
    for(int i = 1 ; i <= n ; i++) {
            a[i] = Int;
            if(i == 1) {
            pos.pb(a[i]);
            res[id].pb(a[i]);
        } else {
            int f = index(pos,a[i]);
            if(f<0) f = 0 ;
            if(f == (int)pos.size()) {
                pos.pb(a[i]);
                res[++id].pb(a[i]);
            } else {
                pos[f] = a[i] ;
                res[f].pb(a[i]);
            }
        }
    }
   // cout<<(int)pos.size()<<endl;
    for(int i = 0 ; i <= id ; i++) {
        for(int j = 0 ; j < (int)res[i].size() ; j++) {
            printf("%d ",res[i][j]);
        }
        puts("");
    }
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
// Policy based data structure
typedef tree<int, null_type,
			less_equal<int>, rb_tree_tag,
			tree_order_statistics_node_update>
	indexed_set;
#define ll long long int
#define pii pair<ll,ll>
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define vi vector<ll>
#define vii vector<pii>
#define all(x) x.begin(),x.end()
#define eb emplace_back
#define yes cout<<"YES"<<endl; return;
#define no cout<<"NO"<<endl; return;
#define flus fflush(stdin); fflush(stdout);
#define F first
#define S second
#define np next_permutation
#define inf 1e18
#define mod 1000000007
#define N 200009
#define pi (double)2*acos(0.0)
#define minpq priority_queue <ll, vector<ll>, greater<ll>>
#define maxpq priority_queue<ll>
void sout(){
	cout<<endl;
}
template <typename T,typename... Types>
void sout(T var1,Types... var2){
	cout<<var1<<" ";
	sout(var2...);
}
/*******************************************************************/
vi sol[N];
vi v;
ll lbound(ll x){
	if(v.size()==0)
		return 0;
	ll l=0,r=v.size()-1;
	ll ind=v.size();
	while(l<=r){
		ll mid=(l+r)/2;
		if(v[mid]<x){
			ind=mid;
			r=mid-1;
		} else{
			l=mid+1;
		}
	}
	return ind;
}
void solve(){
	ll n;
	scanf("%lld",&n);
	vi a(n);
	rep(i,0,n){
		scanf("%lld",&a[i]);
	}
	// rep(i,0,n) cout<<a[i]<<" "; cout<<endl;
	rep(i,0,n){
		ll ind=lbound(a[i]);
		if(ind==v.size()){
			v.push_back(a[i]);
			sol[ind].push_back(a[i]);
		} else{
			v[ind]=a[i];
			sol[ind].push_back(a[i]);
		}
	}
	rep(i,0,N){
		if(sol[i].size()==0)
			break;
		for(auto ele:sol[i]){
			printf("%lld ",ele);
		}
		printf("\n");
	}
}

int main(){
	int t;
	// cin>>t;
	t=1;
	while(t--){
		solve();
	}
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
// Policy based data structure
typedef tree<int, null_type,
			less_equal<int>, rb_tree_tag,
			tree_order_statistics_node_update>
	indexed_set;
#define ll long long int
#define pii pair<ll,ll>
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define vi vector<ll>
#define vii vector<pii>
#define all(x) x.begin(),x.end()
#define eb emplace_back
#define yes cout<<"YES"<<endl; return;
#define no cout<<"NO"<<endl; return;
#define flus fflush(stdin); fflush(stdout);
#define F first
#define S second
#define np next_permutation
#define inf 1e18
#define mod 1000000007
#define N 200009
#define pi (double)2*acos(0.0)
#define minpq priority_queue <ll, vector<ll>, greater<ll>>
#define maxpq priority_queue<ll>
void sout(){
	cout<<endl;
}
template <typename T,typename... Types>
void sout(T var1,Types... var2){
	cout<<var1<<" ";
	sout(var2...);
}
/*******************************************************************/
vi sol[N];
vi v;
ll lbound(ll x){
	if(v.size()==0)
		return 0;
	ll l=0,r=v.size()-1;
	ll ind=v.size();
	while(l<=r){
		ll mid=(l+r)/2;
		if(v[mid]<x){
			ind=mid;
			r=mid-1;
		} else{
			l=mid+1;
		}
	}
	return ind;
}
void solve(){
	ll n;
	cin>>n;
	vi a(n);
	rep(i,0,n){
		cin>>a[i];
	}
	// rep(i,0,n) cout<<a[i]<<" "; cout<<endl;
	rep(i,0,n){
		ll ind=lbound(a[i]);
		if(ind==v.size()){
			v.push_back(a[i]);
			sol[ind].push_back(a[i]);
		} else{
			v[ind]=a[i];
			sol[ind].push_back(a[i]);
		}
	}
	rep(i,0,N){
		if(sol[i].size()==0)
			break;
		for(auto ele:sol[i]){
			printf("%lld ",ele);
		}
		printf("\n");
	}
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t;
	// cin>>t;
	t=1;
	while(t--){
		solve();
	}
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
// Policy based data structure
typedef tree<int, null_type,
			less_equal<int>, rb_tree_tag,
			tree_order_statistics_node_update>
	indexed_set;
#define ll long long int
#define pii pair<ll,ll>
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define vi vector<ll>
#define vii vector<pii>
#define all(x) x.begin(),x.end()
#define eb emplace_back
#define yes cout<<"YES"<<endl; return;
#define no cout<<"NO"<<endl; return;
#define flus fflush(stdin); fflush(stdout);
#define F first
#define S second
#define np next_permutation
#define inf 1e18
#define mod 1000000007
#define N 200009
#define pi (double)2*acos(0.0)
#define minpq priority_queue <ll, vector<ll>, greater<ll>>
#define maxpq priority_queue<ll>
void sout(){
	cout<<endl;
}
template <typename T,typename... Types>
void sout(T var1,Types... var2){
	cout<<var1<<" ";
	sout(var2...);
}
/*******************************************************************/
vi sol[N];
vi v;
ll lbound(ll x){
	if(v.size()==0)
		return 0;
	ll l=0,r=v.size()-1;
	ll ind=v.size();
	while(l<=r){
		ll mid=(l+r)/2;
		if(v[mid]<x){
			ind=mid;
			r=mid-1;
		} else{
			l=mid+1;
		}
	}
	return ind;
}
void solve(){
	ll n;
	cin>>n;
	vi a(n);
	rep(i,0,n){
		cin>>a[i];
	}
	// rep(i,0,n) cout<<a[i]<<" "; cout<<endl;
	rep(i,0,n){
		ll ind=lbound(a[i]);
		if(ind==v.size()){
			v.push_back(a[i]);
			sol[ind].push_back(a[i]);
		} else{
			v[ind]=a[i];
			sol[ind].push_back(a[i]);
		}
	}
	rep(i,0,N){
		if(sol[i].size()==0)
			break;
		for(auto ele:sol[i]){
			cout<<ele<<" ";
		}
		cout<<endl;
	}
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t;
	// cin>>t;
	t=1;
	while(t--){
		solve();
	}
}


#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int, int>

typedef long long ll;

ll mod = 1e9+7;

int n,i,j,k,t;
int a,l,r,mid;
vector<int> v[200005];

int main(){
	scanf("%d", &n);
	scanf("%d", &a);
	v[0].pb(a);
	for(i=k=1;i<n;i++){
		scanf("%d", &a);
		l = 0;
		r = k-1;
		if(a<v[r].back()){
			v[k].pb(a);
			k++;
		}
		else{
			mid = (l+r)/2;
			while(l<r){
				if(a>v[mid].back())
					r = mid;
				else
					l = mid+1;
				mid = (l+r)/2;
			}
			v[l].pb(a);
		}
	}
	for(i=0;i<k;i++){
		for(auto z : v[i])
			printf("%d ", z);
		puts("");
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int, int>

typedef long long ll;

ll mod = 1e9+7;

int n,i,j,k,t;
int a[200005];
map<int, vector<int> > m;

int main(){
	scanf("%d", &n);
	for(i=0;i<n;i++)
		scanf("%d", a+i);
	m[0].pb(a[0]);
	for(i=1,k=0;i<n;i++){
		for(j=0;j<=k;j++){
			if(a[i]>m[j].back())
				break;
		}
		m[j].pb(a[i]);
		if(j>k)
			k++;
	}
	for(auto x : m){
		for(auto z : x.se){
			printf("%d ", z);
		}
		puts("");
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int, int>

typedef long long ll;

ll mod = 1e9+7;

int n,i,j,k,t;
int a[200005];
vector<int> v[200005];

int main(){
	scanf("%d", &n);
	for(i=0;i<n;i++)
		scanf("%d", a+i);
	v[0].pb(a[0]);
	for(i=1,k=0;i<n;i++){
		for(j=0;j<=k;j++){
			if(a[i]>v[j].back())
				break;
		}
		v[j].pb(a[i]);
		if(j>k)
			k++;
	}
	for(i=0;i<=k;i++){
		for(auto z : v[i]){
			printf("%d ", z);
		}
		puts("");
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int, int>

typedef long long ll;

ll mod = 1e9+7;

int n,i,j,k,t;
int a;
vector<int> v[200005];

int main(){
	scanf("%d", &n);
	for(i=0,k=0;i<n;i++){
		scanf("%d", &a);
		for(j=0;j<k;j++){
			if(a>v[j].back())
				break;
		}
		v[j].pb(a);
		if(j>=k)
			k++;
	}
	for(i=0;i<k;i++){
		for(auto z : v[i])
			printf("%d ", z);
		puts("");
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define fir first
#define sec second

#define FAST ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);

int main()
{
    FAST;

    ll n,i,x;
    cin >> n;

    queue<ll> p;
    for(i=0;i<n;i++)
    {
    	cin >> x;
    	p.push(x);
    }

    while(p.size()!=0)
    {
    	queue<ll> q;
    	x=p.front();
	    cout << x << " ";
	    p.pop();
    	while(!p.empty())
    	{
    		if(p.front()>x)
    		{
    			x=p.front();
    			cout << x << " ";
    		}
    		else
    		{
    			q.push(p.front());
    		}
    		p.pop();
    	}
    	p=q;
    	cout << endl;
    }
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define fir first
#define sec second

#define FAST ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);

int main()
{
    FAST;

    ll n,i,j,p,q=1;
    cin >> n;

    vector<vector<ll> > v(n);

    cin >> p;
    v[0].pb(p);
    for(i=1;i<n;i++)
    {
        cin >> p;
        ll ch=0;
        for(j=0;j<q;j++)
        {
            ll l=v[j].size();
            if(v[j][l-1]<p)
            {
                v[j].pb(p);
                ch=1;
                break;
            }
        }

        if(!ch)
        {
            v[q].pb(p);
            q++;
        }
    }

    for(i=0;i<q;i++)
    {
        for(j=0;j<v[i].size();j++)
        {
            cout << v[i][j] << " ";
        }
        cout<<endl;
    }
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define fir first
#define sec second

#define FAST ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);

int main()
{
    FAST;

    ll n,i,j,p,q;
    cin >> n;

    vector<vector<ll> > v(n);
    vector<ll> w;

    cin >> p;
    w.pb(p);
    v[0].pb(p);
    for(i=1;i<n;i++)
    {
        cin >> p;

        if(w[0]<p)
        {
            w[0]=p;
            v[0].pb(p);
        }
        else
        {
            ll l=0,h=w.size(),m;
            while(l<h)
            {
                m=(l+h)/2;

                if(m==w.size()-1 && w[m]>p)
                {
                    v[w.size()].pb(p);
                    w.pb(p);
                    break;
                }
                else if(w[m]<p && w[m-1]>p)
                {
                    w[m]=p;
                    v[m].pb(p);
                    break;
                }
                else if(w[m]>p)
                {
                    l=m+1;
                }
                else if(w[m]<p)
                {
                    h=m+1;
                }
            }
        }
    }

    p=w.size();
    for(i=0;i<p;i++)
    {
        for(j=0;j<v[i].size();j++)
        {
            cout << v[i][j] << " ";
        }
        cout<<endl;
    }
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define fir first
#define sec second

#define FAST ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);

int main()
{
    FAST;

    ll n,i,j,p,q;
    cin >> n;

    vector<vector<ll> > v(n);
    vector<ll> w;

    cin >> p;
    w.pb(p);
    v[0].pb(p);
    for(i=1;i<n;i++)
    {
        cin >> p;

        if(w[0]<p)
        {
            w[0]=p;
            v[0].pb(p);
        }
        else
        {
            ll l=0,h=w.size(),m;
            while(l<=h)
            {
                m=(l+h)/2;

                if(m==w.size()-1 && w[m]>p)
                {
                    v[w.size()].pb(p);
                    w.pb(p);
                    break;
                }
                else if(w[m]<p && w[m-1]>p)
                {
                    w[m]=p;
                    v[m].pb(p);
                    break;
                }
                else if(w[m]>p)
                {
                    l=m+1;
                }
                else if(w[m]<p)
                {
                    h=m-1;
                }
            }
        }
    }

    p=w.size();
    for(i=0;i<p;i++)
    {
        for(j=0;j<v[i].size();j++)
        {
            cout << v[i][j] << " ";
        }
        cout<<endl;
    }
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define fir first
#define sec second

#define FAST ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);

int main()
{
    FAST;

    ll n,i,x;
    cin >> n;

    queue<ll> p;
    for(i=0;i<n;i++)
    {
    	cin >> x;
    	p.push(x);
    }

    while(p.size()!=0)
    {
    	queue<ll> q;
    	x=p.front();
	    cout << x << " ";
	    p.pop();
	    i=p.size();
    	while(i>0)
    	{
    		if(p.front()>x)
    		{
    			x=p.front();
    			cout << x << " ";
    		}
    		else
    		{
    			p.push(p.front());
    		}
    		p.pop();
    		i--;
    	}
    	cout << endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
#define fr(i,a,b) for(i=a; i<=b; i++)
#define fr1(i,a,b) for(i=a; i>=b; i--)
#define ll long long int
#define ld long double
#define vi vector<ll>
#define vii vector <pair<ll,ll> >
#define viii vector <pair<ll,pair<ll,ll> > >
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define mp(i,j) make_pair(i,j)
#define fi first
#define se second
#define input(a,n) fr(i,1,n)cin>>a[i]
#define output(a,n) fr(i,0,n)cout<<a[i]<<endl
#define dbg cout<<"hurr"<<endl;
#define dbg2 cout<<"hurr2"<<endl;
#define md 1000000007
#define INF 9999999999999
#define sp << " " <<
#define endl "\n"
#define pi 3.14159265358979323846
#define fixd(x) cout << fixed << setprecision(x);
#define sz size()
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define tr(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
bool compare(pair<pair<ll,ll>,ll> p1,pair<pair<ll,ll>,ll>p2){
return p1.fi.fi<p2.fi.fi;
}
int main()
{
    fast
    ll n,i,ans,x,j,k;
    cin>>n;
    ll a[n+1],d[n+1];
    vector<ll> v[n+1];
    input(a,n);
    v[0].pb(a[1]);
    d[0]=a[1];
    j=0;
    fr(i,2,n)
    {
        ll l=0;
        ll r=j;
        ll maxi=0;
        while(l<=r)
        {
            ll mid=l+(r-l)/2;
            if(d[mid]<=a[i])
            {
                maxi=max(maxi,d[mid]);
                r=mid-1;
            }
            else
            {
                l=mid+1;
            }
        }
        if(maxi==0)
        {
            j++;
            v[j].pb(a[i]);
            d[j]=a[i];
        }
        fr(k,0,j)
        {
            if(d[k]==maxi)
            {
                v[k].pb(a[i]);
                d[k]=a[i];
                break;
            }
        }
    }
    for(i=0;i<=j;i++)
    {
        for(auto x:v[i])
        {
            cout<<x<<" ";
        }
        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
#define fr(i,a,b) for(i=a; i<=b; i++)
#define fr1(i,a,b) for(i=a; i>=b; i--)
#define ll long long int
#define ld long double
#define vi vector<ll>
#define vii vector <pair<ll,ll> >
#define viii vector <pair<ll,pair<ll,ll> > >
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define mp(i,j) make_pair(i,j)
#define fi first
#define se second
#define input(a,n) fr(i,1,n)cin>>a[i]
#define output(a,n) fr(i,0,n)cout<<a[i]<<endl
#define dbg cout<<"hurr"<<endl;
#define dbg2 cout<<"hurr2"<<endl;
#define md 1000000007
#define INF 9999999999999
#define sp << " " <<
#define endl "\n"
#define pi 3.14159265358979323846
#define fixd(x) cout << fixed << setprecision(x);
#define sz size()
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define tr(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
bool compare(pair<pair<ll,ll>,ll> p1,pair<pair<ll,ll>,ll>p2){
return p1.fi.fi<p2.fi.fi;
}
int main()
{
    fast
    ll n,i,ans,x,j,k;
    cin>>n;
    ll a[n+1],d[n+1];
    vector<ll> v[n+1];
    input(a,n);
    v[0].pb(a[1]);
    d[0]=a[1];
    j=0;
    fr(i,2,n)
    {
        if(a[i]<d[j])
        {
            j++;
            v[j].pb(a[i]);
            d[j]=a[i];
        }
        else
        {
        ll l=0;
        ll r=j;
        ll maxi=0;
        while(l<=r)
        {
            ll mid=l+(r-l)/2;
            if(d[mid]<=a[i])
            {
                maxi=max(maxi,mid);
                r=mid-1;
            }
            else
            {
                l=mid+1;
            }
        }
            v[maxi].pb(a[i]);
            d[maxi]=a[i];
        }
    }
    for(i=0;i<=j;i++)
    {
        for(auto x:v[i])
        {
            cout<<x<<" ";
        }
        cout<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
#define fr(i,a,b) for(i=a; i<=b; i++)
#define fr1(i,a,b) for(i=a; i>=b; i--)
#define ll long long int
#define ld long double
#define vi vector<ll>
#define vii vector <pair<ll,ll> >
#define viii vector <pair<ll,pair<ll,ll> > >
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define mp(i,j) make_pair(i,j)
#define fi first
#define se second
#define input(a,n) fr(i,1,n)cin>>a[i]
#define output(a,n) fr(i,0,n)cout<<a[i]<<endl
#define dbg cout<<"hurr"<<endl;
#define dbg2 cout<<"hurr2"<<endl;
#define md 1000000007
#define INF 9999999999999
#define sp << " " <<
#define endl "\n"
#define pi 3.14159265358979323846
#define fixd(x) cout << fixed << setprecision(x);
#define sz size()
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define tr(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
bool compare(pair<pair<ll,ll>,ll> p1,pair<pair<ll,ll>,ll>p2){
return p1.fi.fi<p2.fi.fi;
}
int main()
{
    fast
    ll n,i,ans,x,j,k;
    cin>>n;
    ll a[n+1],d[n+1];
    vector<ll> v[n+1];
    input(a,n);
    v[0].pb(a[1]);
    d[0]=a[1];
    j=0;
    fr(i,2,n)
    {
        if(a[i]<d[j])
        {
            j++;
            v[j].pb(a[i]);
            d[j]=a[i];
        }
        else
        {
        ll l=0;
        ll r=j;
        ll maxi=INT_MAX;
        while(l<=r)
        {
            ll mid=l+(r-l)/2;
            if(d[mid]<=a[i])
            {
                maxi=min(maxi,mid);
                r=mid-1;
            }
            else
            {
                l=mid+1;
            }
        }
        v[maxi].pb(a[i]);
        d[maxi]=a[i];
        }
    }
    for(i=0;i<=j;i++)
    {
        for(auto x:v[i])
        {
            cout<<x<<" ";
        }
        cout<<endl;
    }
    return 0;
}


#include<iostream>
#include<vector>
#include<utility>
#include<cmath>
#include<map>
#include<set>
#include<queue>
#include<string>
#include <algorithm>
#include <cmath>
#include<iomanip>
#include<limits>
#include <cstring>
#include <string>
#include <functional>
#include <climits>
#include <list>

//#include <bits/stdc++.h>

using namespace std;

const int maxn = (int)(2e5) + 2;
const int mod = (int)(1e9) + 7;

vector <int> ans[maxn];
int d[maxn];
int a[maxn], n;

int main() {

		cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];

	int c = n;
	for (int i = 1; i <= n; i++) {
		int ind = lower_bound(d + 1, d + 1 + n, a[i]) - d;
		d[ind - 1] = a[i];
		ans[ind - 1].push_back(a[i]);
	}
	for (int i = n; i >= 1; i--) {
		for (int j = 0; j < ans[i].size(); j++)
			cout << ans[i][j] << " ";
		cout << endl;
	}
	return 0;
}


#include<iostream>
#include<vector>
#include<utility>
#include<cmath>
#include<map>
#include<set>
#include<queue>
#include<string>
#include <algorithm>
#include <cmath>
#include<iomanip>
#include<limits>
#include <cstring>
#include <string>
#include <functional>
#include <climits>
#include <list>

//#include <bits/stdc++.h>

using namespace std;

int main() {
	long long n;

	int big;
	cin >> n;
	vector<vector<int>>v(n);
	cin >> big;
	long long cnt = 0;
	cout << big << " ";
	//v[cnt].push_back(big);

	for (int i = 1; i<n; i++)
	{
		int x;
		cin >> x;
		if (x>big)
		{
			cout << x << " ";
			big = x;

		}
		else {
			int k = 0; bool kk = false;
			while (!kk)
			{
				if (k <= cnt)
				{
					if (v[k][v[k].size() - 1]<x)
					{
						v[k].push_back(x);
						kk = true;
						// big=x;
					}
					else k++;
				}
				else { cnt++; v[cnt].push_back(x); kk = true; }
			}
		}
	}

	//	cin>>big;
	//	cout<<big;
	for (int i = 0; i<=cnt; i++)
	{
		for (int j = 0; j<v[i].size(); j++)
		{
			cout << v[i][j] << " ";
		}
		cout << endl;
	}

}


#include<iostream>
#include<vector>
#include<utility>
#include<cmath>
#include<map>
#include<set>
#include<queue>
#include<string>
#include <algorithm>
#include <cmath>
#include<iomanip>
#include<limits>
#include <cstring>
#include <string>
#include <functional>
#include <climits>
#include <list>

//#include <bits/stdc++.h>

using namespace std;

int main() {
	long long n;

	int big;
	cin >> n;
	vector<vector<int>>v(n);
	cin >> big;
	long long cnt = 0;
	cout << big << " ";
	//v[cnt].push_back(big);

	for (int i = 1; i<n; i++)
	{
		int x;
		cin >> x;
		if (x>big)
		{
			cout << x << " ";
			big = x;

		}
		else {
			int k = 0; bool kk = false;
			while (!kk)
			{
				if (v[k].size() > 0  )
				{
					if (k <= cnt)
					{
						if (v[k][v[k].size() - 1] < x)
						{
							v[k].push_back(x);
							kk = true;
							// big=x;
						}
						else k++;
					}
					else { cnt++; v[cnt].push_back(x); kk = true; }
				}
				else if(k <= cnt){ v[cnt].push_back(x); kk = true; }
				else { cnt++; v[cnt].push_back(x); kk = true; }

			}
		}
	}
	cout << endl;
	//	cin>>big;
	//	cout<<big;
	for (int i = 0; i<=cnt; i++)
	{
		for (int j = 0; j<v[i].size(); j++)
		{
			cout << v[i][j] << " ";
		}
		cout << endl;
	}

}


#include<iostream>
#include<vector>
#include<utility>
#include<cmath>
#include<map>
#include<set>
#include<queue>
#include<string>
#include <algorithm>
#include <cmath>
#include<iomanip>
#include<limits>
#include <cstring>
#include <string>
#include <functional>
#include <climits>
#include <list>

//#include <bits/stdc++.h>

using namespace std;

int main() {
	long long n;

	int big;
	cin >> n;
	vector<vector<int>>v(n);
	cin >> big;
	long long cnt = 0;
	v[cnt].push_back(big);

	for (int i = 1; i<n; i++)
	{
		int x;
		cin >> x;
		if (x>big)
		{
			int k = 0; bool kk = false;
			while (!kk)
			{
				if (k <= cnt)
				{
					if (v[k][v[k].size() - 1]<x)
					{
						v[k].push_back(x);
						kk = true;
						big = x;
					}
					else k++;
				}
				// {cnt++;v[cnt].push_back
			}
		}
		else {
			int k = 0; bool kk = false;
			while (!kk)
			{
				if (k <= cnt)
				{
					if (v[k][v[k].size() - 1]<x)
					{
						v[k].push_back(x);
						kk = true;
						// big=x;
					}
					else k++;
				}
				else { cnt++; v[cnt].push_back(x); kk = true; }
			}
		}
	}

	//	cin>>big;
	//	cout<<big;
	for (int i = 0; i<=cnt; i++)
	{
		for (int j = 0; j<v[i].size(); j++)
		{
			cout << v[i][j] << " ";
		}
		cout << endl;
	}

}


#include<iostream>
#include<vector>
#include<utility>
#include<cmath>
#include<map>
#include<set>
#include<queue>
#include<string>
#include <algorithm>
#include <cmath>
#include<iomanip>
#include<limits>
#include <cstring>
#include <string>
#include <functional>
#include <climits>
#include <list>

//#include <bits/stdc++.h>

using namespace std;

const int maxn = (int)(2e5)+2;
const int mod = (int)(1e9)+7;

vector <int> ans[maxn];
int d[maxn];
int a[maxn], n;

int main() {
	_

	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];

	int c = n;
	for (int i = 1; i <= n; i++) {
		int ind = lower_bound(d+1,d+1+n,a[i])-d;
		d[ind-1]= a[i];
		ans[ind-1].pb(a[i]);
	}
	for (int i = n; i >= 1; i--) {
	 	for (int j = 0; j < ans[i].size(); j++)
	 		cout << ans[i][j] << " ";
	 	cout << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
	// your code goes here
	cin.tie(NULL);
	ios_base::sync_with_stdio(false);

	int n;
	long long int prev;
	long long int a[200006];
    cin>>n;
    for(int i=0;i<n;i++)
    cin>>a[i];
    //cout<<"here";
    vector<int> an[200006];
    int ind=0,k=0,f1;
    for(int i=0;i<n;i++)
    { f1=0;
            if(i!=0&&a[i]>an[k-1].back())
            {for(int j=0;j<k;j++)
            {
                if(a[i]>(an[j].back()))
                {an[j].push_back(a[i]);
                f1=1;
                break;
                }
            }
            }

        if(f1==0)
        {

            an[k].push_back(a[i]);
            k++;
        }
    }
    for(int i=0;i<k;i++)
    {
        for(vector<int>::iterator it = an[i].begin() ; it != an[i].end(); ++it)
        cout<<*it<<" ";

        cout<<endl;
    }

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
	// your code goes here
	cin.tie(NULL);
	ios_base::sync_with_stdio(false);

	int n;
	long long int prev;
	long long int a[200006];
    cin>>n;
    for(int i=0;i<n;i++)
    cin>>a[i];
    //cout<<"here";
    vector<int> an[200006];
    int ind=0,k=0,f1;
    for(int i=0;i<n;i++)
    { f1=0;
            if(i!=0&&a[i]>an[k-1].back())
            for(int j=0;j<k;j++)
            {
                if(a[i]>(an[j].back()))
                {an[j].push_back(a[i]);
                f1=1;
                break;
                }
            }

        if(f1==0)
        {

            an[k].push_back(a[i]);
            k++;
        }
    }
    for(int i=0;i<k;i++)
    {
        for(vector<int>::iterator it = an[i].begin() ; it != an[i].end(); ++it)
        cout<<*it<<" ";

        cout<<endl;
    }

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
	// your code goes here
	cin.tie(NULL);
	ios_base::sync_with_stdio(false);

	int n;
	long long int prev;
	long long int a[200006],f[200006]={0};
    cin>>n;
    for(int i=0;i<n;i++)
    cin>>a[i];
    int ind=0;

    while(ind<=n&&f[ind]==0)

    {    int fi =0;

        cout<<a[ind]<<" ";
        prev=a[ind];
        f[ind]=1;
       for(int i=ind+1;i<n;i++)
       {
        if(f[i]==0&&a[i]>prev)
        {
            cout<<a[i]<<" ";
            f[i]=1;
            prev=a[i];
        }
        if(fi==0)
        {if(f[i]==0&&a[i]<prev)
        {
         ind=i;
         fi=1;
        }
        }

        }
       // cout<<ind;
       cout<<endl;

    }

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
	// your code goes here
	cin.tie(NULL);
	ios_base::sync_with_stdio(false);

	int n;
	long long int prev;
	long long int a[200006];
    cin>>n;
    for(int i=0;i<n;i++)
    cin>>a[i];
    //cout<<"here";
    vector<int> an[200006];
    int ind=0,k=0,f1;
    for(int i=0;i<n;i++)
    { f1=0;

            for(int j=0;j<k;j++)
            {
                if(a[i]>(an[j].back()))
                {an[j].push_back(a[i]);
                f1=1;
                break;
                }
            }

        if(f1==0)
        {

            an[k].push_back(a[i]);
            k++;
        }
    }
    for(int i=0;i<k;i++)
    {
        for(vector<int>::iterator it = an[i].begin() ; it != an[i].end(); ++it)
        cout<<*it<<" ";

        cout<<endl;
    }

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
	// your code goes here
	cin.tie(NULL);
	ios_base::sync_with_stdio(false);

	int n;
	long long int prev;
	long long int a[200006];
    cin>>n;
    for(int i=0;i<n;i++)
    cin>>a[i];
    //cout<<"here";
    vector<int> an[200006];
    int ind=0,k=0,f1;
    for(int i=0;i<n;i++)
    { f1=0;
            if(i!=0&&a[i]>an[k-1].back())
            {int l,mid,h,ind;
            l=0;h=k-1;
            while(l<=h)
            {
                mid=(l+h)/2;
                if(an[mid].back()<a[i])
                {
                    ind=mid;
                    h=mid-1;
                }
                else
                {
                    l=mid+1;
                }
            }
            an[ind].push_back(a[i]);
            f1=1;
            }

        if(f1==0)
        {

            an[k].push_back(a[i]);
            k++;
        }
    }
    for(int i=0;i<k;i++)
    {
        for(vector<int>::iterator it = an[i].begin() ; it != an[i].end(); ++it)
        cout<<*it<<" ";

        cout<<endl;
    }

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,a[200100],nex[200100],pre[200100],b,cnt,maxi[200100];
void Delete(int x)
{
    if(pre[x]==0 && nex[x]==0)
    {

    }
    else if(pre[x]==0)
    {
        pre[nex[x]]=0;
        b=nex[x];
    }
    else if(nex[x]==0)
    {
        nex[pre[x]]=0;
    }
    else
    {
        nex[pre[x]]=nex[x];
        pre[nex[x]]=pre[x];
    }
    cnt--;

}

int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        nex[i]=i+1;
        pre[i]=i-1;
    }
    maxi[n]=a[n];
    for(int i=n-1;i>=1;i--) maxi[i]=max(a[i],maxi[i+1]);
    cnt=n;
    nex[n]=0;
    b=1;
    int prev=a[b],x=b;
    while(cnt>0)
    {
        cout<<a[b];
        prev=a[b];
        x=nex[b];
        Delete(b);
        if(cnt==0) break;
        while(x!=0)
        {
            if(a[x]==maxi[x])
            {
                cout<<" "<<a[x];
                Delete(x);
                prev=a[x];
                break;
            }
            else if(a[x]>prev && a[x])
            {
                cout<<" "<<a[x];
                Delete(x);
                prev=a[x];
            }
            else x=nex[x];
        }
        cout<<endl;

    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,last[200100],index;
vector<int> v[200100];
int I(int x,int n)
{
    if(x<last[n] || n==0) return -1;
    int b=1,e=n;
    while(b<=e)
    {
        if(e-b<=1 && e>=b)
        {
            if(last[b]<x) return b;
            if(last[e]<x) return e;
            break;
        }
        else
        {
            int mid=(b+e+1)/2;
            if(last[mid]>x) b=mid+1;
            else e=mid;
        }

    }

}

int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        int x,p;
        cin>>x;
        p=I(x,index);
        if(p==-1)
        {
            index++;
            v[index].push_back(x);
            last[index]=x;
        }
        else
        {
            v[p].push_back(x);
            last[p]=x;
        }

    }
    for(int i=1;i<=index;i++)
    {
        for(int k=0;k<v[i].size();k++) cout<<v[i][k]<<" ";
        cout<<endl;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,a[200100],nex[200100],pre[200100],b,cnt,maxi[200100];
void Delete(int x)
{
    if(pre[x]==0 && nex[x]==0)
    {

    }
    else if(pre[x]==0)
    {
        pre[nex[x]]=0;
        b=nex[x];
    }
    else if(nex[x]==0)
    {
        nex[pre[x]]=0;
    }
    else
    {
        nex[pre[x]]=nex[x];
        pre[nex[x]]=pre[x];
    }
    cnt--;

}

int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        nex[i]=i+1;
        pre[i]=i-1;
    }
    maxi[n]=a[n];
    for(int i=n-1;i>=1;i--) maxi[i]=max(a[i],maxi[i+1]);
    cnt=n;
    nex[n]=0;
    b=1;
    int prev=a[b],x=b;
    while(cnt>0)
    {
        cout<<a[b];
        int Maxi=maxi[b],B=b;
        prev=a[b];
        x=nex[b];
        Delete(b);
        if(a[B]==Maxi)
        {
            cout<<endl;
            continue;
        }
        if(cnt==0) break;
        while(x!=0)
        {
            if(a[x]==maxi[x])
            {
                cout<<" "<<a[x];
                Delete(x);
                prev=a[x];
                break;
            }
            else if(a[x]>prev && a[x])
            {
                cout<<" "<<a[x];
                Delete(x);
                prev=a[x];
            }
            else x=nex[x];
        }
        cout<<endl;

    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,a[200100],nex[200100],pre[200100],b,cnt;
void Delete(int x)
{
    if(pre[x]==0 && nex[x]==0)
    {

    }
    else if(pre[x]==0)
    {
        pre[nex[x]]=0;
        b=nex[x];
    }
    else if(nex[x]==0)
    {
        nex[pre[x]]=0;
    }
    else
    {
        nex[pre[x]]=nex[x];
        pre[nex[x]]=pre[x];
    }
    cnt--;

}

int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        nex[i]=i+1;
        pre[i]=i-1;
    }
    cnt=n;
    nex[n]=0;
    b=1;
    int prev=a[b],x=b;
    while(cnt>0)
    {
        cout<<a[b];
        prev=a[b];
        x=nex[b];
        Delete(b);
        if(cnt==0) break;
        while(x!=0){
        if(a[x]>prev)
        {
            cout<<" "<<a[x];
            Delete(x);
            prev=a[x];
        }
        else x=nex[x];
        }
        cout<<endl;

    }

    return 0;
}


#include<iostream>
#include<bits/stdc++.h>
#include<math.h>
#include<vector>
#include<string>
#include<cstring>
#include<ctype.h>
using namespace std;

typedef long long ll;
typedef long l;

#define EPS 1e-9
#define pb push_back
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define mp make_pair
#define ff first
#define ss second
#define umpit unordered_map<ll,ll>::iterator
#define mpit map<ll,ll>::iterator
#define setit set<ll>::iterator
#define all(v) v.begin(),v.end()

void dfs(vector<ll>adj[], unordered_map<ll,ll>mp1, ll s, bool visited[])
{
	cout<<mp1[s]<<" ";
	visited[s] = true;
	for(auto e: adj[s])
	{
		if(!visited[e])
		{
			dfs(adj,mp1,e,visited);
		}
	}
}

int main()
{
	ll n;
	cin>>n;
	vector<ll>v(n),temp;
	FOR(i,0,n)
	{
		cin>>v[i];
	}
	ll rank = 1;
	temp = v;
	sort(all(temp));//o nlogn
	unordered_map<ll,ll>mp1,mp2;
	FOR(i,0,n)
	{
		mp1[temp[i]] = rank++;//get rank
		mp2[rank-1] = temp[i];//get actual val
	}
	vector<ll>v1;
	v1 = v;
	FOR(i,0,n){
		v1[i] = mp1[v1[i]];//v1 is rank array.....
	}
	///////
	/////
	///////
	/////
	//////
	/////
	//////
	/////////////////////

	vector<ll>adj[n+1];//based on rank
	set<ll>s1;
	s1.insert(v1[0]);
	ll Min = v1[0];
	//O( nlogn )
	FOR(i,1,n)
	{
		if(v1[i] < Min)
		{
			Min = v1[i];
			s1.insert(v1[i]);
		}
		else if(v1[i] > Min)
		{
			setit it;
			it = s1.upper_bound(v1[i]);
			it--;// <=
			//get pos
			adj[*it].pb(v1[i]);//one way edges
			s1.erase(it);// Amortized
			s1.insert(v1[i]);//logn
			Min = *s1.begin();
		}
	}

	bool visited[n+1];
	memset(visited,false, n+1);
	FOR(i,0,n)//o n
	{
		if(!visited[v1[i]]){
			dfs(adj, mp2, v1[i],visited);
			cout<< endl;
		}

	}
	return 0;
}


#include<iostream>
#include<bits/stdc++.h>
#include<math.h>
#include<vector>
#include<string>
#include<cstring>
#include<ctype.h>
using namespace std;

typedef long long ll;
typedef long l;

#define EPS 1e-9
#define pb push_back
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define mp make_pair
#define ff first
#define ss second
#define umpit unordered_map<ll,ll>::iterator
#define mpit map<ll,ll>::iterator
#define setit set<ll>::iterator
#define all(v) v.begin(),v.end()

void dfs(vector<ll>adj[], unordered_map<ll,ll>mp1, ll s, bool visited[])
{
	printf("%lld ", mp1[s]);
	visited[s] = true;
	for(auto e: adj[s])
	{
		if(!visited[e])
		{
			dfs(adj,mp1,e,visited);
		}
	}
}

int main()
{
	ll n;
	scanf("%lld",&n);
	vector<ll>v(n),temp;
	FOR(i,0,n)
	{
		scanf("%lld",&v[i]);

	}
	ll rank = 1;
	temp = v;
	sort(all(temp));//o nlogn
	unordered_map<ll,ll>mp1,mp2;
	FOR(i,0,n)
	{
		mp1[temp[i]] = rank++;//get rank
		mp2[rank-1] = temp[i];//get actual val
	}
	vector<ll>v1;
	v1 = v;
	FOR(i,0,n){
		v1[i] = mp1[v1[i]];//v1 is rank array.....
	}
	///////
	/////
	///////
	/////
	//////
	/////
	//////
	/////////////////////

	vector<ll>adj[n+1];//based on rank
	set<ll>s1;
	s1.insert(v1[0]);
	ll Min = v1[0];
	//O( nlogn )
	FOR(i,1,n)
	{
		if(v1[i] < Min)
		{
			Min = v1[i];
			s1.insert(v1[i]);
		}
		else if(v1[i] > Min)
		{
			setit it;
			it = s1.upper_bound(v1[i]);
			it--;// <=
			//get pos
			adj[*it].pb(v1[i]);//one way edges
			s1.erase(it);// Amortized
			s1.insert(v1[i]);//logn
			Min = *s1.begin();
		}
	}

	bool visited[n+1];
	FOR(i,0,n+1)
	{
		visited[i] = false;
	}
	FOR(i,0,n)//o n
	{
		if(!visited[v1[i]]){
			dfs(adj, mp2, v1[i],visited);
			printf("\n");
		}

	}
	return 0;
}


#include<iostream>
#include<bits/stdc++.h>
#include<math.h>
#include<vector>
#include<string>
#include<cstring>
#include<ctype.h>
using namespace std;

typedef long long ll;
typedef long l;

#define EPS 1e-9
#define pb push_back
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define mp make_pair
#define ff first
#define ss second
#define umpit unordered_map<ll,ll>::iterator
#define mpit map<ll,ll>::iterator
#define setit set<ll>::iterator
#define all(v) v.begin(),v.end()

int main()
{
	ll n;
	cin>>n;
	vector<ll>v(n);
	FOR(i,0,n)
	{
		cin>>v[i];
		v[i] = -v[i];
	}
	vector<vector<ll> >adj(1,vector<ll>(1,v[0]));
	vector<ll>top;
	top.pb(v[0]);
	FOR(i,1,n)
	{
		if(v[i] > top.back())
		{
			// give its own space
			vector<ll>x;
			x.pb(v[i]);
			adj.pb(x);
			top.pb(v[i]);
		}
		else
		{
			ll j = lower_bound(all(top), v[i]) - top.begin();
			adj[j].pb(v[i]);
			top[j] = v[i];
		}
	}
	FOR(i,0,adj.size())
	{
		for(auto e: adj[i])
		{
			cout<<-e<<" ";
		}
		cout<<endl;
	}
	return 0;
}


#include<iostream>
#include<bits/stdc++.h>
#include<math.h>
#include<vector>
#include<string>
#include<cstring>
#include<ctype.h>
using namespace std;

typedef long long ll;
typedef long l;

#define EPS 1e-9
#define pb push_back
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define mp make_pair
#define ff first
#define ss second
#define umpit unordered_map<ll,ll>::iterator
#define mpit map<ll,ll>::iterator
#define setit set<ll>::iterator
#define all(v) v.begin(),v.end()

void dfs(vector<ll>adj[], map<ll,ll>mp1, ll s, bool visited[])
{
	cout<<mp1[s]<<" ";
	visited[s] = true;
	for(auto e: adj[s])
	{
		if(!visited[e])
		{
			dfs(adj,mp1,e,visited);
		}
	}
}

int main()
{
	ll n;
	cin>>n;
	vector<ll>v(n),temp;
	FOR(i,0,n)
	{
		cin>>v[i];
	}
	ll rank = 1;
	temp = v;
	sort(all(temp));//o nlogn
	unordered_map<ll,ll>mp1,mp2;
	FOR(i,0,n)
	{
		mp1[temp[i]] = rank++;//get rank
		mp2[rank-1] = temp[i];//get actual val
	}
	vector<ll>v1;
	v1 = v;
	FOR(i,0,n){
		v1[i] = mp1[v1[i]];//v1 is rank array.....
	}
	///////
	/////
	///////
	/////
	//////
	/////
	//////
	/////////////////////

	vector<ll>adj[n+1];//based on rank
	set<ll>s1;
	s1.insert(v1[0]);
	ll Min = v1[0];
	//O( nlogn )
	FOR(i,1,n)
	{
		if(v1[i] < Min)
		{
			Min = v1[i];
			s1.insert(v1[i]);
		}
		else if(v1[i] > Min)
		{
			setit it;
			it = s1.upper_bound(v1[i]);
			it--;// <=
			//get pos
			adj[*it].pb(v1[i]);//one way edges
			s1.erase(it);// Amortized
			s1.insert(v1[i]);//logn
			Min = *s1.begin();
		}
	}

	bool visited[n+1];
	memset(visited,false, n+1);
	FOR(i,0,n)//o n
	{
		if(!visited[v1[i]]){
			dfs(adj, mp2, v1[i],visited);
			cout<< endl;
		}

	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
int t,n,i,ok,sel[105],m,j,k;
string s[105],sol,aux;
void solve()
{

}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie();
    cin>>n>>m;
    for(i=1; i<=n; i++)
        cin>>s[i];
    for(i=1; i<n; i++)
        for(j=i+1; j<=n; j++)
        {
            bool ok=1;
            for(k=0; k<m; k++)
                if(s[i][k]!=s[j][m-k-1])
                {
                    ok=0;
                    break;
                }
            if(ok)
            {
                sel[i]=1;
                sel[j]=1;
                sol+=s[i];
            }
        }
    bool ok=0;
    for(i=1; i<=n; i++)
        if(!sel[i])
        {
            bool ok1=1;
            for(j=0; j<m; j++)
                if(s[i][j]!=s[i][m-j-1])
                {
                    ok1=0;
                    break;
                }
            if(ok1)
            {
                ok=1;
                break;
            }
        }
    if(sol.size()!=0)
        if(ok)
        {
            aux=sol;
            reverse(aux.begin(),aux.end());
            sol=sol+s[i]+aux;
            cout<<sol.size()<<'\n';
            cout<<sol;
        }
        else
        {
            aux=sol;
            reverse(aux.begin(),aux.end());
            sol=sol+aux;
            cout<<sol.size();
            cout<<sol;
        }
    else cout<<0;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
int t,n,i,ok,sel[105],m,j,k;
string s[105],sol,aux;
void solve()
{

}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie();
    cin>>n>>m;
    for(i=1; i<=n; i++)
        cin>>s[i];
    for(i=1; i<n; i++)
        for(j=i+1; j<=n; j++)
        {
            bool ok=1;
            for(k=0; k<m/2+1; k++)
                if(s[i][k]!=s[j][m-k-1])
                {
                    ok=0;
                    break;
                }
            if(ok)
            {
                sel[i]=1;
                sel[j]=1;
                sol+=s[i];
            }
        }
    bool ok=0;
    for(i=1; i<=n; i++)
        if(!sel[i])
        {
            bool ok1=1;
            for(j=0; j<m/2+1; j++)
                if(s[i][j]!=s[i][m-j-1])
                {
                    ok1=0;
                    break;
                }
            if(ok1)
            {
                ok=1;
                break;
            }
        }
    if(sol.size()!=0)
        if(ok)
        {
            aux=sol;
            reverse(aux.begin(),aux.end());
            sol=sol+s[i]+aux;
            cout<<sol.size()<<'\n';
            cout<<sol;
        }
        else
        {
            aux=sol;
            reverse(aux.begin(),aux.end());
            sol=sol+aux;
            cout<<sol.size()<<'\n';
            cout<<sol;
        }
    else cout<<0;
    return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
#define ll int
#define maxn 40010
using namespace std;
int T,n,m,tmp=0;
int a[1010];
char s[110][110],ans[100010];
int read()
{
    int x=0,f=1;
    char c=getchar();
    while(c>'9'||c<'0')
    {
        if(c=='-')
        {
            f=-1;
        }
        c=getchar();
    }
    while(c>='0'&&c<='9')
    {
        x=x*10+c-'0';
        c=getchar();
    }
    return x*f;
}
bool find(int x,int y)
{
	for(int i=1;i<=m;i++)
	{
		if(s[x][i]!=s[y][m-i+1])
			return false;
	}
	return true;
}
bool get(int x)
{
	for(int i=1;i<=m;i++)
	{
		if(s[x][i]!=s[x][m-i+1])
			return false;
	}
	return true;
}
int main()
{
	int x,y,z,flag=0;
	n=read();
	m=read();
	for(int i=1;i<=n;i++)
	{
		scanf("%s",s[i]+1);
	}
	for(int i=1;i<=n;i++)
	for(int j=1;j<i;j++)
	if(!a[i]&&!a[j])
	{
		if(find(i,j))
		{
			a[i]=1;
			a[j]=1;
			for(int k=1;k<=m;k++)
			{
				ans[++tmp]=s[i][k];
			}
		}
	}
	for(int i=1;i<=n;i++)
	if(get(i)&&!a[i])
	{
		for(int j=1;j<=m;j++)
		{
			ans[++tmp]=s[i][j];
		}
		flag=1;
		break;
	}
	int l=tmp-m,r=tmp+1;
	if(flag==0)
		l=tmp;
	while(l)
	{
		ans[r]=ans[l];
		l--;
		r++;
	}
	if(r==1)
	{
		printf("0\n");
	}
	else
	{
		printf("%d\n",r-1);
		for(int i=1;i<r;i++)
			printf("%c",ans[i]);
		printf("\n");
	}

    return 0;
}
/*
6 3
tab
one
bat
tab
one
bat

*/


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
#define ll int
#define maxn 40010
using namespace std;
int T,n,m,tmp=0;
int a[1010],b[1010];
char s[110][110],ans[100010];
int read()
{
    int x=0,f=1;
    char c=getchar();
    while(c>'9'||c<'0')
    {
        if(c=='-')
        {
            f=-1;
        }
        c=getchar();
    }
    while(c>='0'&&c<='9')
    {
        x=x*10+c-'0';
        c=getchar();
    }
    return x*f;
}
bool find(int x,int y)
{
	for(int i=1;i<=m;i++)
	{
		if(s[x][i]!=s[y][m-i+1])
			return false;
	}
	return true;
}
bool get(int x)
{
	for(int i=1;i<=m;i++)
	{
		if(s[x][i]!=s[x][m-i+1])
			return false;
	}
	return true;
}
int main()
{
	int x,y,z,flag=0;
	n=read();
	m=read();
	for(int i=1;i<=n;i++)
	{
		scanf("%s",s[i]+1);
	}
	for(int i=1;i<=n;i++)
	for(int j=1;j<i;j++)
	if(!b[i]&&!b[j])
	{
		if(find(i,j))
		{
			for(int k=1;k<=m;k++)
			{
				ans[++tmp]=s[i][k];
			}
		}
	}
	for(int i=1;i<=n;i++)
	if(get(i)&&!a[i])
	{
		for(int j=1;j<=m;j++)
		{
			ans[++tmp]=s[i][j];
		}
		flag=1;
		break;
	}
	int l=tmp-m,r=tmp+1;
	if(flag==0)
		l=tmp;
	while(l)
	{
		ans[r]=ans[l];
		l--;
		r++;
	}
	if(r==1)
	{
		printf("0\n");
	}
	else
	{
		printf("%d\n",r-1);
		for(int i=1;i<=r;i++)
			printf("%c",ans[i]);
		printf("\n");
	}

    return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
#define ll int
#define maxn 40010
using namespace std;
int T,n,m,tmp=0;
int a[1010],b[1010];
char s[110][110],ans[100010];
int read()
{
    int x=0,f=1;
    char c=getchar();
    while(c>'9'||c<'0')
    {
        if(c=='-')
        {
            f=-1;
        }
        c=getchar();
    }
    while(c>='0'&&c<='9')
    {
        x=x*10+c-'0';
        c=getchar();
    }
    return x*f;
}
bool find(int x,int y)
{
	for(int i=1;i<=m;i++)
	{
		if(s[x][i]!=s[y][m-i+1])
			return false;
	}
	return true;
}
bool get(int x)
{
	for(int i=1;i<=m;i++)
	{
		if(s[x][i]!=s[x][m-i+1])
			return false;
	}
	return true;
}
int main()
{
	int x,y,z,flag=0;
	n=read();
	m=read();
	for(int i=1;i<=n;i++)
	{
		scanf("%s",s[i]+1);
	}
	for(int i=1;i<=n;i++)
	for(int j=1;j<i;j++)
	if(!b[i]&&!b[j])
	{
		if(find(i,j))
		{
			b[i]=1;
			b[j]=1;
			for(int k=1;k<=m;k++)
			{
				ans[++tmp]=s[i][k];
			}
		}
	}
	for(int i=1;i<=n;i++)
	if(get(i)&&!a[i])
	{
		for(int j=1;j<=m;j++)
		{
			ans[++tmp]=s[i][j];
		}
		flag=1;
		break;
	}
	int l=tmp-m,r=tmp+1;
	if(flag==0)
		l=tmp;
	while(l)
	{
		ans[r]=ans[l];
		l--;
		r++;
	}
	if(r==1)
	{
		printf("0\n");
	}
	else
	{
		printf("%d\n",r-1);
		for(int i=1;i<=r;i++)
			printf("%c",ans[i]);
		printf("\n");
	}

    return 0;
}
/*
6 3
tab
one
bat
tab
one
bat

*/


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
#define ll int
#define maxn 40010
using namespace std;
int T,n,m,tmp=0;
int a[1010];
char s[110][110],ans[100010];
int read()
{
    int x=0,f=1;
    char c=getchar();
    while(c>'9'||c<'0')
    {
        if(c=='-')
        {
            f=-1;
        }
        c=getchar();
    }
    while(c>='0'&&c<='9')
    {
        x=x*10+c-'0';
        c=getchar();
    }
    return x*f;
}
bool find(int x,int y)
{
	for(int i=1;i<=m;i++)
	{
		if(s[x][i]!=s[y][m-i+1])
			return false;
	}
	return true;
}
bool get(int x)
{
	for(int i=1;i<=m;i++)
	{
		if(s[x][i]!=s[x][m-i+1])
			return false;
	}
	return true;
}
int main()
{
	int x,y,z,flag=0;
	n=read();
	m=read();
	for(int i=1;i<=n;i++)
	{
		scanf("%s",s[i]+1);
	}
	for(int i=1;i<=n;i++)
	for(int j=1;j<i;j++)
	if(!a[i]&&!a[j])
	{
		if(find(i,j))
		{
			a[i]=1;
			a[j]=1;
			for(int k=1;k<=m;k++)
			{
				ans[++tmp]=s[i][k];
			}
		}
	}
	for(int i=1;i<=n;i++)
	if(get(i)&&!a[i])
	{
		for(int j=1;j<=m;j++)
		{
			ans[++tmp]=s[i][j];
		}
		flag=1;
		break;
	}
	int l=tmp-m,r=tmp+1;
	if(flag==0)
		l=tmp;
	while(l)
	{
		ans[r]=ans[l];
		l--;
		r++;
	}
	if(r==1)
	{
		printf("0\n");
	}
	else
	{
		printf("%d\n",r-1);
		for(int i=1;i<=r;i++)
			printf("%c",ans[i]);
		printf("\n");
	}

    return 0;
}
/*
6 3
tab
one
bat
tab
one
bat

*/


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
#define ll int
#define maxn 40010
using namespace std;
int T,n,m,tmp=0;
int a[1010];
char s[110][110],ans[100010];
int read()
{
    int x=0,f=1;
    char c=getchar();
    while(c>'9'||c<'0')
    {
        if(c=='-')
        {
            f=-1;
        }
        c=getchar();
    }
    while(c>='0'&&c<='9')
    {
        x=x*10+c-'0';
        c=getchar();
    }
    return x*f;
}
bool find(int x,int y)
{
	for(int i=1;i<=m;i++)
	{
		if(s[x][i]!=s[y][m-i+1])
			return false;
	}
	return true;
}
bool get(int x)
{
	for(int i=1;i<=m;i++)
	{
		if(s[x][i]!=s[x][m-i+1])
			return false;
	}
	return true;
}
int main()
{
	int x,y,z,flag=0;
	n=read();
	m=read();
	for(int i=1;i<=n;i++)
	{
		scanf("%s",s[i]+1);
	}
	for(int i=1;i<=n;i++)
	for(int j=1;j<i;j++)
	if(!a[i]&&!a[j])
	{
		if(find(i,j))
		{
			a[i]=1;
			a[j]=1;
			for(int k=1;k<=m;k++)
			{
				ans[++tmp]=s[i][k];
			}
		}
	}
	for(int i=1;i<=n;i++)
	if(get(i)&&!a[i])
	{
		for(int j=1;j<=m;j++)
		{
			ans[++tmp]=s[i][j];
		}
		flag=1;
		break;
	}
	int l=tmp-m,r=tmp+1;
	if(flag==0)
		l=tmp;
	while(l)
	{
		ans[r]=ans[l];
		l--;
		r++;
	}
	if(r==1)
	{
		printf("0\n");
	}
	else
	{
		printf("%d\n",r-1);
		for(int i=1;i<=r;i++)
			printf("%c",ans[i]);
//		printf("\n");
	}

    return 0;
}
/*
6 3
tab
one
bat
tab
one
bat

*/


//good luck
# include <iostream>
# include <cstdio>
# include <cmath>
# include <cstdlib>
# include <cstring>
# include <string>
# include <algorithm>
# include <vector>
# include <queue>
# include <ctime>
# include <map>

#define LL long long
#define ULL unsigned long long
#define maxn int(1e6+5)
#define is(a) (a>='0'&&a<='9')
#define iis(a) (a>='a'&&a<='z')
#define iabs(a) ((a)>0?(a):(-a))
#define imax(a,b) ((a)>(b)?(a):(b))
#define imin(a,b) ((a)<(b)?(a):(b))

using namespace std;

struct data{ULL x;int y;};

vector <data> d[maxn];

int N,M,t,que[maxn],ans[maxn],k;

char st[100+5][100+5];

const int p=23332;
const int Mod=3e5+7;

inline void read(int &x)
{
  x=0;bool f=0;char ch=getchar();
  for (;!is(ch);ch=getchar()) f|=ch=='-';
  for (;is(ch);ch=getchar()) x=(x<<3)+(x<<1)+(ch^48);
  x=f?-x:x;
}

inline void reads(int x)
{
  int len=0;char ch=getchar();
  for (;!iis(ch);ch=getchar());
  for (;iis(ch);ch=getchar()) st[x][++len]=ch;
}

inline void insert(ULL x,int y)
{
	int u=x%Mod;
	for (int i = 0; i < d[u].size(); ++i)
		if (d[u][i].x==x) return;
	d[u].push_back((data){x,y});
}

inline int query(ULL x)
{
	int u=x%Mod;
	for (int i = 0; i < d[u].size(); ++i)
		if (d[u][i].x==x) return d[u][i].y;
	return 0;
}

int main()
{
//	freopen("a.in","r",stdin);
	read(N); read(M);
	for (int i = 1; i <= N; ++i)
	{
		reads(i);
		ULL x=0,y=0;
		for (int j = 1; j <= M; ++j) x=x*p+st[i][j]-'a'+1;
		for (int j = M; j >= 1; --j) y=y*p+st[i][j]-'a'+1;
		if (x==y) {k=i;continue;}
		if (query(y)) que[++t]=query(y),que[++t]=i;
		else		  insert(x,i);

	}
	if (k) ++t;
	for (int i = 1,j = 1; i < t; i += 2)
	{
		ans[j] = que[i];
		ans[t - j + 1] = que[i + 1];
		++j;
	}
	if (k) ans[t / 2 + 1] = k;
	printf("%d\n",t*M);
	for (int i = 1; i <= t; ++i)
	{
		for (int j = 1; j <= M; ++j)
		putchar(st[ans[i]][j]);
	}
    return 0;
}


//good luck
# include <iostream>
# include <cstdio>
# include <cmath>
# include <cstdlib>
# include <cstring>
# include <string>
# include <algorithm>
# include <vector>
# include <queue>
# include <ctime>
# include <map>

#define LL long long
#define ULL unsigned long long
#define maxn int(1e6+5)
#define is(a) (a>='0'&&a<='9')
#define iis(a) (a>='a'&&a<='z')
#define iabs(a) ((a)>0?(a):(-a))
#define imax(a,b) ((a)>(b)?(a):(b))
#define imin(a,b) ((a)<(b)?(a):(b))

using namespace std;

struct data{ULL x;int y;};

vector <data> d[maxn];

int N,M,t,que[maxn],ans[maxn],k;

char st[100+5][100+5];

const int p=19260817;
const int Mod=3e5+7;

inline void read(int &x)
{
  x=0;bool f=0;char ch=getchar();
  for (;!is(ch);ch=getchar()) f|=ch=='-';
  for (;is(ch);ch=getchar()) x=(x<<3)+(x<<1)+(ch^48);
  x=f?-x:x;
}

inline void reads(int x)
{
  int len=0;char ch=getchar();
  for (;!iis(ch);ch=getchar());
  for (;iis(ch);ch=getchar()) st[x][++len]=ch;
}

inline void insert(ULL x,int y)
{
	int u=x%Mod;
	for (int i = 0; i < d[u].size(); ++i)
		if (d[u][i].x==x) return;
	d[u].push_back((data){x,y});
}

inline int query(ULL x)
{
	int u=x%Mod;
	for (int i = 0; i < d[u].size(); ++i)
		if (d[u][i].x==x) return d[u][i].y;
	return 0;
}

int main()
{
//	freopen("a.in","r",stdin);
	read(N); read(M);
	for (int i = 1; i <= N; ++i)
	{
		reads(i);
		ULL x=0,y=0;
		for (int j = 1; j <= M; ++j) x=x*p+st[i][j]-'a'+1;
		for (int j = M; j >= 1; --j) y=y*p+st[i][j]-'a'+1;
		if (x==y) {k=i;continue;}
		if (query(y)) que[++t]=query(y),que[++t]=i;
		else		  insert(x,i);

	}
	if (k) ++t;
	for (int i = 1,j = 1; i < t; i += 2)
	{
		ans[j] = que[i];
		ans[t - j + 1] = que[i + 1];
		++j;
	}
	if (k) ans[t / 2 + 1] = k;
	printf("%d\n",t*M);
	for (int i = 1; i <= t; ++i)
	{
		for (int j = 1; j <= M; ++j)
		putchar(st[ans[i]][j]);
	}
    return 0;
}


//good luck
# include <iostream>
# include <cstdio>
# include <cmath>
# include <cstdlib>
# include <cstring>
# include <string>
# include <algorithm>
# include <vector>
# include <queue>
# include <ctime>
# include <map>

#define LL long long
#define ULL unsigned long long
#define maxn int(1e6+5)
#define is(a) (a>='0'&&a<='9')
#define iis(a) (a>='a'&&a<='z')
#define iabs(a) ((a)>0?(a):(-a))
#define imax(a,b) ((a)>(b)?(a):(b))
#define imin(a,b) ((a)<(b)?(a):(b))

using namespace std;

struct data{ULL x;int y;};

vector <data> d[maxn];

int N,M,t,que[maxn],ans[maxn],k;

char st[100+5][100+5];

const int p=23332;
const int Mod=3e5+7;

inline void read(int &x)
{
  x=0;bool f=0;char ch=getchar();
  for (;!is(ch);ch=getchar()) f|=ch=='-';
  for (;is(ch);ch=getchar()) x=(x<<3)+(x<<1)+(ch^48);
  x=f?-x:x;
}

inline void reads(int x)
{
  int len=0;char ch=getchar();
  for (;!iis(ch);ch=getchar());
  for (;iis(ch);ch=getchar()) st[x][++len]=ch;
}

inline void insert(ULL x,int y)
{
	int u=x%Mod;
	for (int i = 0; i < d[u].size(); ++i)
		if (d[u][i].x==x) return;
	d[u].push_back((data){x,y});
}

inline int query(ULL x)
{
	int u=x%Mod;
	for (int i = 0; i < d[u].size(); ++i)
		if (d[u][i].x==x) return d[u][i].y;
	return 0;
}

int main()
{
//	freopen("a.in","r",stdin);
	read(N); read(M);
	for (int i = 1; i <= N; ++i)
	{
		reads(i);
		ULL x=0,y=0;
		for (int j = 1; j <= M; ++j) x=x*p+st[i][j];
		for (int j = M; j >= 1; --j) y=y*p+st[i][j];
		if (x==y) {k=i;continue;}
		if (query(y)) que[++t]=query(y),que[++t]=i;
		else		  insert(x,i);

	}
	if (k) ++t;
	for (int i = 1,j = 1; i < t; i += 2)
	{
		ans[j] = que[i];
		ans[t - j + 1] = que[i + 1];
		++j;
	}
	if (k) ans[t / 2 + 1] = k;
	printf("%d\n",t*M);
	for (int i = 1; i <= t; ++i)
	{
		for (int j = 1; j <= M; ++j)
		putchar(st[ans[i]][j]);
	}
    return 0;
}


//good luck
# include <iostream>
# include <cstdio>
# include <cmath>
# include <cstdlib>
# include <cstring>
# include <string>
# include <algorithm>
# include <vector>
# include <queue>
# include <ctime>
# include <map>

#define LL long long
#define ULL unsigned long long
#define maxn int(1e6+5)
#define is(a) (a>='0'&&a<='9')
#define iis(a) (a>='a'&&a<='z')
#define iabs(a) ((a)>0?(a):(-a))
#define imax(a,b) ((a)>(b)?(a):(b))
#define imin(a,b) ((a)<(b)?(a):(b))

using namespace std;

struct data{int x,y;};

vector <data> d[maxn];

int N,M,t,que[maxn],ans[maxn],k;

char st[100+5][100+5];

const int p=232;
const int Mod=3e5+7;

inline void read(int &x)
{
  x=0;bool f=0;char ch=getchar();
  for (;!is(ch);ch=getchar()) f|=ch=='-';
  for (;is(ch);ch=getchar()) x=(x<<3)+(x<<1)+(ch^48);
  x=f?-x:x;
}

inline void reads(int x)
{
  int len=0;char ch=getchar();
  for (;!iis(ch);ch=getchar());
  for (;iis(ch);ch=getchar()) st[x][++len]=ch;
}

inline void insert(ULL x,int y)
{
	int u=x%Mod;
	for (int i = 0; i < d[u].size(); ++i)
		if (d[u][i].x==x) return;
	d[u].push_back((data){x,y});
}

inline int query(ULL x)
{
	int u=x%Mod;
	for (int i = 0; i < d[u].size(); ++i)
		if (d[u][i].x==x) return d[u][i].y;
	return 0;
}

int main()
{
//	freopen("a.in","r",stdin);
	read(N); read(M);
	for (int i = 1; i <= N; ++i)
	{
		reads(i);
		ULL x=0,y=0;
		for (int j = 1; j <= M; ++j) x=x*p+st[i][j];
		for (int j = M; j >= 1; --j) y=y*p+st[i][j];
		if (query(y)) que[++t]=query(y),que[++t]=i;
		else		  insert(x,i);
		if (x==y) k=i;
	}
	if (k) ++t;
	for (int i = 1,j = 1; i < t; i += 2)
	{
		ans[j] = que[i];
		ans[t - j + 1] = que[i + 1];
		++j;
	}
	if (k) ans[t / 2 + 1] = k;
	printf("%d\n",t*M);
	for (int i = 1; i <= t; ++i)
	{
		for (int j = 1; j <= M; ++j)
		putchar(st[ans[i]][j]);
	}
    return 0;
}


//good luck
# include <iostream>
# include <cstdio>
# include <cmath>
# include <cstdlib>
# include <cstring>
# include <string>
# include <algorithm>
# include <vector>
# include <queue>
# include <ctime>
# include <map>

#define LL long long
#define ULL unsigned long long
#define maxn int(1e6+5)
#define is(a) (a>='0'&&a<='9')
#define iis(a) (a>='a'&&a<='z')
#define iabs(a) ((a)>0?(a):(-a))
#define imax(a,b) ((a)>(b)?(a):(b))
#define imin(a,b) ((a)<(b)?(a):(b))

using namespace std;

struct data{ULL x;int y;};

vector <data> d[maxn];

int N,M,t,que[maxn],ans[maxn],k;

char st[100+5][100+5];

const int p=232;
const int Mod=3e5+7;

inline void read(int &x)
{
  x=0;bool f=0;char ch=getchar();
  for (;!is(ch);ch=getchar()) f|=ch=='-';
  for (;is(ch);ch=getchar()) x=(x<<3)+(x<<1)+(ch^48);
  x=f?-x:x;
}

inline void reads(int x)
{
  int len=0;char ch=getchar();
  for (;!iis(ch);ch=getchar());
  for (;iis(ch);ch=getchar()) st[x][++len]=ch;
}

inline void insert(ULL x,int y)
{
	int u=x%Mod;
	for (int i = 0; i < d[u].size(); ++i)
		if (d[u][i].x==x) return;
	d[u].push_back((data){x,y});
}

inline int query(ULL x)
{
	int u=x%Mod;
	for (int i = 0; i < d[u].size(); ++i)
		if (d[u][i].x==x) return d[u][i].y;
	return 0;
}

int main()
{
//	freopen("a.in","r",stdin);
	read(N); read(M);
	for (int i = 1; i <= N; ++i)
	{
		reads(i);
		ULL x=0,y=0;
		for (int j = 1; j <= M; ++j) x=x*p+st[i][j];
		for (int j = M; j >= 1; --j) y=y*p+st[i][j];
		if (query(y)) que[++t]=query(y),que[++t]=i;
		else		  insert(x,i);
		if (x==y) k=i;
	}
	if (k) ++t;
	for (int i = 1,j = 1; i < t; i += 2)
	{
		ans[j] = que[i];
		ans[t - j + 1] = que[i + 1];
		++j;
	}
	if (k) ans[t / 2 + 1] = k;
	printf("%d\n",t*M);
	for (int i = 1; i <= t; ++i)
	{
		for (int j = 1; j <= M; ++j)
		putchar(st[ans[i]][j]);
	}
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

vector<string> contem;
vector<string> ispalindrome;
vector<bool> usando(300, false);

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	vector<string> strings;

	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		string s;
		cin >> s;
		strings.push_back(s);
	}

	for (int i = 0; i < n; i++) {
		bool astringemquestaoeumastringpalindromeounaoireiverificaragora = true;
		for (int j = 0; j < m; j++) {
			if (strings[i][j] != strings[i][m-j-1])
				astringemquestaoeumastringpalindromeounaoireiverificaragora = false;
		}
		if (astringemquestaoeumastringpalindromeounaoireiverificaragora) {
			ispalindrome.push_back(strings[i]);
		}

	}

	for (int i = 0; i < n; i++) {
		if (usando[i]) continue;
		for (int j = i+1; j < n; j++) {
			if (usando[j]) continue;
			bool sao = true;
			for (int k = 0; k < m; k++) {
				if (strings[i][k] != strings[j][m-k-1]) sao = false;
				if (!sao) break;
			}
			if (sao) {
				contem.push_back(strings[i]);
				usando[j] = true;
				break;
			}
		}
	}

	string res = "";

	for (string &s : contem) {
		res += s;
	}
	for (string &s : ispalindrome) {res += s; break;}
	for (string &s : contem) {
		reverse(s.begin(), s.end());
		res += s;
	}
	cout << res.length() << endl;
	cout << res << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

vector<string> contem;
vector<string> ispalindrome;
vector<bool> usando(300, false);

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	vector<string> strings;

	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		string s;
		cin >> s;
		strings.push_back(s);
	}

	for (int i = 0; i < n; i++) {
		bool astringemquestaoeumastringpalindromeounaoireiverificaragora = true;
		for (int j = 0; j < m; j++) {
			if (strings[i][j] != strings[i][m-j-1])
				astringemquestaoeumastringpalindromeounaoireiverificaragora = false;
		}
		if (astringemquestaoeumastringpalindromeounaoireiverificaragora) {
			ispalindrome.push_back(srings[i]);
		}

	}

	for (int i = 0; i < n; i++) {
		if (usando[i]) continue;
		for (int j = i+1; j < n; j++) {
			if (usando[j]) continue;
			bool sao = true;
			for (int k = 0; k < m; k++) {
				if (strings[i][k] != strings[j][m-k-1]) sao = false;
				if (!sao) break;
			}
			if (sao) {
				contem.push_back(strings[i]);
				usando[j] = true;
				break;
			}
		}
	}

	string res = "";

	for (string &s : contem) {
		res += s;
	}
	for (string &s : ispalindrome) res += s;
	for (string &s : contem) {
		reverse(s.begin(), s.end());
		res += s;
	}
	cout << res.length() << endl;
	cout << res << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

vector<string> contem;
vector<bool> usando(300, false);

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	vector<string> strings;

	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		string s;
		cin >> s;
		strings.push_back(s);
	}

	for (int i = 0; i < n; i++) {
		if (usando[i]) continue;
		for (int j = i+1; j < n; j++) {
			if (usando[j]) continue;
			bool sao = true;
			for (int k = 0; k < m; k++) {
				if (strings[i][k] != strings[j][m-k-1]) sao = false;
				if (!sao) break;
			}
			if (sao) {
				contem.push_back(strings[i]);
				usando[j] = true;
				break;
			}
		}
	}

	string res = ""

	for (string &s : contem) {
		res += s;
	}
	for (string &s : contem) {
		reverse(s.begin(), s.end());
		res += s;
	}
	cout << res.length() << endl;
	cout << res << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

vector<string> contem;
vector<string> ispalindrome;
vector<bool> usando(300, false);

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	vector<string> strings;

	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		string s;
		cin >> s;
		strings.push_back(s);
	}

	for (int i = 0; i < n; i++) {
		bool astringemquestaoeumastringpalindromeounaoireiverificaragora = true;
		for (int j = 0; j < m; j++) {
			if (strings[i][j] != strings[i][m-j-1])
				astringemquestaoeumastringpalindromeounaoireiverificaragora = false;
		}
		if (astringemquestaoeumastringpalindromeounaoireiverificaragora) {
			ispalindrome.push_back(strings[i]);
		}

	}

	for (int i = 0; i < n; i++) {
		if (usando[i]) continue;
		for (int j = i+1; j < n; j++) {
			if (usando[j]) continue;
			bool sao = true;
			for (int k = 0; k < m; k++) {
				if (strings[i][k] != strings[j][m-k-1]) sao = false;
				if (!sao) break;
			}
			if (sao) {
				contem.push_back(strings[i]);
				usando[j] = true;
				break;
			}
		}
	}

	string res = "";

	for (string s : contem) {
		res += s;
	}
	for (string s : ispalindrome) {res += s; break;}
	for (int i = contem.size()-1; i >= 0; i--) {
		reverse(contem[i].begin(), contem[i].end());
		res += contem[i];
	}
	cout << res.length() << endl;
	cout << res << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

vector<string> contem;
vector<string> ispalindrome;
vector<bool> usando(300, false);

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	vector<string> strings;

	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		string s;
		cin >> s;
		strings.push_back(s);
	}

	for (int i = 0; i < n; i++) {
		bool astringemquestaoeumastringpalindromeounaoireiverificaragora = true;
		for (int j = 0; j < m; j++) {
			if (strings[i][j] != strings[i][m-j-1])
				astringemquestaoeumastringpalindromeounaoireiverificaragora = false;
		}
		if (astringemquestaoeumastringpalindromeounaoireiverificaragora) {
			ispalindrome.push_back(strings[i]);
		}

	}

	for (int i = 0; i < n; i++) {
		if (usando[i]) continue;
		for (int j = i+1; j < n; j++) {
			if (usando[j]) continue;
			bool sao = true;
			for (int k = 0; k < m; k++) {
				if (strings[i][k] != strings[j][m-k-1]) sao = false;
				if (!sao) break;
			}
			if (sao) {
				contem.push_back(strings[i]);
				usando[j] = true;
				break;
			}
		}
	}

	string res = "";

	for (string &s : contem) {
		res += s;
	}
	for (string &s : ispalindrome) res += s;
	for (string &s : contem) {
		reverse(s.begin(), s.end());
		res += s;
	}
	cout << res.length() << endl;
	cout << res << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

vector<string> contem;
vector<bool> usando(300, false);

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	vector<string> strings;

	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		string s;
		cin >> s;
		strings.push_back(s);
	}

	for (int i = 0; i < n; i++) {
		if (usando[i]) continue;
		for (int j = i+1; j < n; j++) {
			if (usando[j]) continue;
			bool sao = true;
			for (int k = 0; k < m; k++) {
				if (strings[i][k] != strings[j][m-k-1]) sao = false;
				if (!sao) break;
			}
			if (sao) {
				contem.push_back(strings[i]);
				usando[j] = true;
				break;
			}
		}
	}

	string res = "";

	for (string &s : contem) {
		res += s;
	}
	for (string &s : contem) {
		reverse(s.begin(), s.end());
		res += s;
	}
	cout << res.length() << endl;
	cout << res << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

vector<string> contem;
vector<bool> usando(300, false);

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	vector<string> strings;

	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		string s;
		cin >> s;
		strings.push_back(s);
	}

	for (int i = 0; i < n; i++) {
		if (usando[i]) continue;
		for (int j = i+1; j < n; j++) {
			if (usando[j]) continue;
			bool sao = true;
			for (int k = 0; k < m; k++) {
				if (strings[i][k] != strings[j][m-k-1]) sao = false;
				if (!sao) break;
			}
			if (sao) {
				contem.push_back(strings[i]);
				usando[j] = true;
				break;
			}
		}
	}

	for (string &s : contem) {
		cout << s;
	}
	for (string &s : contem) {
		reverse(s.begin(), s.end());
		cout << s;
	}
	cout << endl;

	return 0;
}


/* be name Khoda */
// HW
#include <bits/stdc++.h>
using namespace std;

const int N = 110;
int n, m, match[N], pal = -1;
string s[N];
vector<pair<int, int>> v;

int main() {
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> s[i];
		match[i] = -1;
	}
	for (int i = 0; i < n; i++) {
		if (match[i] == -1) {
			reverse(s[i].begin(), s[i].end());
			string s2 = s[i];
			reverse(s[i].begin(), s[i].end());
//			cout << i << "  " << s2 << "\n";
			for (int j = i + 1; j < n; j++) {
				if (s[j] == s2) {
					v.push_back({i, j});
					match[i] = j;
					match[j] = i;
					break;
				}
			}
		}
	}
	for (int i = 0; i < n; i++) {
		if (match[i] == -1) {
			reverse(s[i].begin(), s[i].end());
			string s2 = s[i];
			reverse(s[i].begin(), s[i].end());
			if (s2 == s[i]) {
//				cout << s[i] << " 54135 \n";
				pal = i;
				break;
			}
		}
	}
	int size = (pal >= 0) + 2 * v.size();
	cout << size * m << "\n";
	if (size % 2) {
		int mid = size / 2;
		for (int i = 0; i < mid; i++) {
			auto t = v[i];
			cout << s[t.first];
		}
		cout << s[pal];
		for (int i = mid + 1; i < size; i++) {
				auto t = v[i - mid - 1];
				cout << s[t.second];
		}
	}
	else {
		int mid = size / 2;
		for (int i = 0; i < mid; i++) {
			auto t = v[i];
			cout << s[t.first];
		}
		for (int i = mid; i < size; i++) {
			auto t = v[i - mid];
			cout << s[t.second];
		}
	}
}


/* be name Khoda */
// HW
#include <bits/stdc++.h>
using namespace std;

const int N = 110;
int n, m, match[N], pal = -1;
string s[N];
vector<pair<int, int>> v;

int main() {
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> s[i];
		match[i] = -1;
	}
	for (int i = 0; i < n; i++) {
		if (match[i] == -1) {
			reverse(s[i].begin(), s[i].end());
			string s2 = s[i];
			reverse(s[i].begin(), s[i].end());
//			cout << i << "  " << s2 << "\n";
			for (int j = i + 1; j < n; j++) {
				if (s[j] == s2) {
					v.push_back({i, j});
					match[i] = j;
					match[j] = i;
					break;
				}
			}
		}
	}
	for (int i = 0; i < n; i++) {
		if (match[i] == -1) {
			reverse(s[i].begin(), s[i].end());
			string s2 = s[i];
			reverse(s[i].begin(), s[i].end());
			if (s2 == s[i]) {
//				cout << s[i] << " 54135 \n";
				pal = i;
				break;
			}
		}
	}
	/* for (int i = 0; i < v.size(); i++) {
		cout << i << "  " << s[v[i].first] << "  " << s[v[i].second] << " -- \n";
	} */
	int size = (pal >= 0) + 2 * v.size();
	cout << size * m << "\n";
	if (size % 2) {
		int mid = size / 2;
		for (int i = 0; i < mid; i++) {
			auto t = v[i];
			cout << s[t.first];
		}
		cout << s[pal];
		for (int i = mid + 1; i < size; i++) {
				auto t = v[size - 1 - i];
				cout << s[t.second];
		}
	}
	else {
		int mid = size / 2;
		for (int i = 0; i < mid; i++) {
			auto t = v[i];
			cout << s[t.first];
		}
		for (int i = mid; i < size; i++) {
			auto t = v[size - 1 - i];
			cout << s[t.second];
		}
	}
}


/* be name Khoda */
// HW
#include <bits/stdc++.h>
using namespace std;

const int N = 110;
int n, m, match[N], pal = -1;
string s[N];
vector<pair<int, int>> v;

int main() {
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> s[i];
		match[i] = -1;
	}
	for (int i = 0; i < n; i++) {
		if (match[i] == -1) {
			reverse(s[i].begin(), s[i].end());
			string s2 = s[i];
			reverse(s[i].begin(), s[i].end());
//			cout << i << "  " << s2 << "\n";
			for (int j = i + 1; j < n; j++) {
				if (s[j] == s2) {
					v.push_back({i, j});
					match[i] = j;
					match[j] = i;
					break;
				}
			}
		}
	}
	for (int i = 0; i < n; i++) {
		if (match[i] == -1) {
			reverse(s[i].begin(), s[i].end());
			string s2 = s[i];
			reverse(s[i].begin(), s[i].end());
			if (s2 == s[i]) {
				pal = i;
				break;
			}
		}
	}
	int size = (pal >= 0) + 2 * v.size();
	cout << size * m << "\n";
	if (size % 2) {
		int mid = size / 2;
		for (int i = 0; i < mid; i++) {
			auto t = v[i];
			cout << s[t.first];
		}
		cout << s[mid];
		for (int i = mid + 1; i < size; i++) {
				auto t = v[i - mid - 1];
				cout << s[t.second];
		}
	}
	else {
		int mid = size / 2;
		for (int i = 0; i < mid; i++) {
			auto t = v[i];
			cout << s[t.first];
		}
		for (int i = mid; i < size; i++) {
			auto t = v[i - mid];
			cout << s[t.second];
		}
	}
}


/* be name Khoda */
// HW
#include <bits/stdc++.h>
using namespace std;

const int N = 110;
int n, m, match[N], pal = -1;
string s[N];
vector<pair<int, int>> v;

int main() {
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> s[i];
		match[i] = -1;
	}
	for (int i = 0; i < n; i++) {
		if (match[i] == -1) {
			reverse(s[i].begin(), s[i].end());
			string s2 = s[i];
			reverse(s[i].begin(), s[i].end());
//			cout << i << "  " << s2 << "\n";
			for (int j = i + 1; j < n; j++) {
				if (s[j] == s2) {
					v.push_back({i, j});
					match[i] = j;
					match[j] = i;
					break;
				}
			}
		}
	}
	for (int i = 0; i < n; i++) {
		if (match[i] == -1) {
			reverse(s[i].begin(), s[i].end());
			string s2 = s[i];
			reverse(s[i].begin(), s[i].end());
			if (s2 == s[i]) {
//				cout << s[i] << " 54135 \n";
				pal = i;
				break;
			}
		}
	}
	/* for (int i = 0; i < v.size(); i++) {
		cout << i << "  " << s[v[i].first] << "  " << s[v[i].second] << " -- \n";
	} */
	int size = (pal >= 0) + 2 * v.size();
	cout << size * m << "\n";
	if (size % 2) {
		int mid = size / 2;
		for (int i = 0; i < mid; i++) {
			auto t = v[i];
			cout << s[t.first];
		}
		cout << s[pal];
		for (int i = mid + 1; i < size; i++) {
				auto t = v[i - mid - 1 + mid - 1];
				cout << s[t.second];
		}
	}
	else {
		int mid = size / 2;
		for (int i = 0; i < mid; i++) {
			auto t = v[i];
			cout << s[t.first];
		}
		for (int i = mid; i < size; i++) {
			auto t = v[i - mid + mid - 1];
			cout << s[t.second];
		}
	}
}


/* be name Khoda */
// HW
#include <bits/stdc++.h>
using namespace std;

const int N = 110;
int n, m, match[N], pal = -1;
string s[N];
vector<pair<int, int>> v;

int main() {
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> s[i];
		match[i] = -1;
	}
	for (int i = 0; i < n; i++) {
		if (match[i] == -1) {
			reverse(s[i].begin(), s[i].end());
			string s2 = s[i];
			reverse(s[i].begin(), s[i].end());
//			cout << i << "  " << s2 << "\n";
			for (int j = i + 1; j < n; j++) {
				if (s[j] == s2) {
					v.push_back({i, j});
					match[i] = j;
					match[j] = i;
					break;
				}
			}
		}
	}
	for (int i = 0; i < n; i++) {
		if (match[i] == -1) {
			reverse(s[i].begin(), s[i].end());
			string s2 = s[i];
			reverse(s[i].begin(), s[i].end());
			if (s2 == s[i]) {
//				cout << s[i] << " 54135 \n";
				pal = i;
				break;
			}
		}
	}
	/* for (int i = 0; i < v.size(); i++) {
		cout << i << "  " << s[v[i].first] << "  " << s[v[i].second] << " -- \n";
	} */
	int size = (pal >= 0) + 2 * v.size();
//	cout << size * m << "\n";
	if (size % 2) {
		int mid = size / 2;
		for (int i = 0; i < mid; i++) {
			auto t = v[i];
			cout << s[t.first];
		}
		cout << s[pal];
		for (int i = mid + 1; i < size; i++) {
				auto t = v[i - mid - 1 + mid - 1];
				cout << s[t.second];
		}
	}
	else {
		int mid = size / 2;
		for (int i = 0; i < mid; i++) {
			auto t = v[i];
			cout << s[t.first];
		}
		for (int i = mid; i < size; i++) {
			auto t = v[i - mid + mid - 1];
			cout << s[t.second];
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
int m,n;
char s[102][52];

int res;
int judge1(int a,int b)
{
	int flag = 1;
	for(int i = 0;i < m;i++)
	{
		if(s[a][i] != s[b][m-i-1])
		{
			flag = 0;
			break;
		}
	}
	if(flag)
	return 1;
	else
	return 0;
}
int judge2(int x)
{
	for(int i = 1;i < m;i++)
	if(s[x][i] != s[x][0])
	return 0;
	return 1;
}
int main()
{
	int count1,count2;
	while(scanf("%d %d",&n,&m) != EOF)
	{
		char a[5002] = "";
		int mp1[52];
     	int mp2[52];
		count1 = 0;
		count2 = 0;
		for(int i = 0;i < n;i++)
		scanf("%s",&s[i]);
		for(int i = 0;i < n-1;i++)
		{
			if(judge2(i))
			{
				mp1[count1] = i;
				count1++;
				continue;
			}
			for(int j = i + 1;j < n;j++)
			{
				if(judge1(i,j))
				{
					mp2[count2] = i;
					mp2[++count2] = j;
					count2++;
					break;
				}
			}
		}
		for(int i = 0;i < count2;i+=2)
		strcat(a,s[mp2[i]]);
		for(int i = 0;i < count1;i++)
		strcat(a,s[mp1[i]]);
		for(int i = count2 - 1;i > 0;i-=2)
		strcat(a,s[mp2[i]]);
		printf("%d\n",(count1 + count2) * m);
		printf("%s\n",a);
	}
}


#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
int m,n;
char s[102][52];
int mp[52];
int res;
int judge1(int a,int b)
{
	int flag = 1;
	for(int i = 0;i < m;i++)
	{
		if(s[a][i] != s[b][m-i-1])
		{
			flag = 0;
			break;
		}
	}
	if(flag)
	return 1;
	else
	return 0;
}
int judge2(int x)
{
	for(int i = 0;i < m/2;i++)
	if(s[x][i] != s[x][m-i-1])
	return 0;
	return 1;
}
int main()
{
	int count1,count2;
	while(scanf("%d %d",&n,&m) != EOF)
	{
		char a[5002] = "";
		memset(mp,INF,sizeof(mp));
		int mp1[52];
     	int mp2[52];
		count1 = 0;
		count2 = 0;
		for(int i = 0;i < n;i++)
		scanf("%s",&s[i]);
		for(int i = 0;i < n-1;i++)
		{
			if(mp[i] != INF)
			continue;
			if(judge2(i))
			{
				mp1[count1] = i;
				count1++;
				continue;
			}
			for(int j = i + 1;j < n;j++)
			{
				if(judge1(i,j))
				{
					mp2[count2] = i;
					mp2[++count2] = j;
					mp[i] = j;
					mp[j] = i;
					count2++;
					break;
				}
			}
		}
		if(judge2(n-1))
		{
			mp1[count1] = n-1;
			count1++;
		}
		for(int i = 0;i < count2;i+=2)
		strcat(a,s[mp2[i]]);
		for(int i = 0;i < count1;i++)
		strcat(a,s[mp1[i]]);
		for(int i = count2 - 1;i > 0;i-=2)
		strcat(a,s[mp2[i]]);
		printf("%d\n",(count1 + count2) * m);
		printf("%s\n",a);
	}
}


#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
int m,n;
char s[105][55];
int mp[105];
int res;
int judge1(int a,int b)
{
	int flag = 1;
	for(int i = 0;i < m;i++)
	{
		if(s[a][i] != s[b][m-i-1])
		{
			flag = 0;
			break;
		}
	}
	if(flag)
	return 1;
	else
	return 0;
}
int judge2(int x)
{
	for(int i = 0;i < m/2;i++)
	if(s[x][i] != s[x][m-i-1])
	return 0;
	return 1;
}
int main()
{
	int count1,count2,flag;
	while(scanf("%d %d",&n,&m) != EOF)
	{
		char a[5100] = "";
		memset(mp,INF,sizeof(mp));
		int mp1[105];
     	int mp2[105];
		count1 = 0;
		count2 = 0;
		for(int i = 0;i < n;i++)
		scanf("%s",&s[i]);
		for(int i = 0;i < n-1;i++)
		{
			if(mp[i] != INF)
			continue;
			flag = 1;
			for(int j = i + 1;j < n;j++)
			{
				if(judge1(i,j))
				{
					mp2[count2] = i;
					mp2[++count2] = j;
					mp[i] = j;
					mp[j] = i;
					count2++;
					flag = 0;
					break;
				}
			}
			if(flag && count1 == 0)
			{
				if(judge2(i))
				{
					mp1[count1] = i;
					count1++;
					continue;
				}
			}
		}
		if(judge2(n-1) && count1 == 0)
		{
			mp1[count1] = n-1;
			count1++;
		}
		for(int i = 0;i < count2;i+=2)
		strcat(a,s[mp2[i]]);
		for(int i = 0;i < count1;i++)
		strcat(a,s[mp1[i]]);
		for(int i = count2 - 1;i > 0;i-=2)
		strcat(a,s[mp2[i]]);
		printf("%d\n",(count1 + count2) * m);
		printf("%s\n",a);
	}
}


#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
int m,n;
char s[102][52];
int mp[102];
int res;
int judge1(int a,int b)
{
	int flag = 1;
	for(int i = 0;i < m;i++)
	{
		if(s[a][i] != s[b][m-i-1])
		{
			flag = 0;
			break;
		}
	}
	if(flag)
	return 1;
	else
	return 0;
}
int judge2(int x)
{
	for(int i = 0;i < m/2;i++)
	if(s[x][i] != s[x][m-i-1])
	return 0;
	return 1;
}
int main()
{
	int count1,count2,flag;
	while(scanf("%d %d",&n,&m) != EOF)
	{
		char a[5020] = "";
		memset(mp,INF,sizeof(mp));
		int mp1[52];
     	int mp2[52];
		count1 = 0;
		count2 = 0;
		for(int i = 0;i < n;i++)
		scanf("%s",&s[i]);
		for(int i = 0;i < n-1;i++)
		{
			if(mp[i] != INF)
			continue;
			flag = 1;
			for(int j = i + 1;j < n;j++)
			{
				if(judge1(i,j))
				{
					mp2[count2] = i;
					mp2[++count2] = j;
					mp[i] = j;
					mp[j] = i;
					count2++;
					flag = 0;
					break;
				}
			}
			if(flag && count1 == 0)
			{
				if(judge2(i))
				{
					mp1[count1] = i;
					count1++;
					continue;
				}
			}
		}
		if(judge2(n-1) && count1 == 0)
		{
			mp1[count1] = n-1;
			count1++;
		}
		for(int i = 0;i < count2;i+=2)
		strcat(a,s[mp2[i]]);
		for(int i = 0;i < count1;i++)
		strcat(a,s[mp1[i]]);
		for(int i = count2 - 1;i > 0;i-=2)
		strcat(a,s[mp2[i]]);
		printf("%d\n",(count1 + count2) * m);
		printf("%s\n",a);
	}
}


#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
int m,n;
char s[102][52];
int mp[52];
int res;
int judge1(int a,int b)
{
	int flag = 1;
	for(int i = 0;i < m;i++)
	{
		if(s[a][i] != s[b][m-i-1])
		{
			flag = 0;
			break;
		}
	}
	if(flag)
	return 1;
	else
	return 0;
}
int judge2(int x)
{
	for(int i = 0;i < m/2;i++)
	if(s[x][i] != s[x][m-i-1])
	return 0;
	return 1;
}
int main()
{
	int count1,count2,flag;
	while(scanf("%d %d",&n,&m) != EOF)
	{
		char a[5002] = "";
		memset(mp,INF,sizeof(mp));
		int mp1[52];
     	int mp2[52];
		count1 = 0;
		count2 = 0;
		for(int i = 0;i < n;i++)
		scanf("%s",&s[i]);
		for(int i = 0;i < n-1;i++)
		{
			if(mp[i] != INF)
			continue;
			flag = 1;
			for(int j = i + 1;j < n;j++)
			{
				if(judge1(i,j))
				{
					mp2[count2] = i;
					mp2[++count2] = j;
					mp[i] = j;
					mp[j] = i;
					count2++;
					flag = 0;
					break;
				}
			}
			if(flag && count1 == 0)
			{
				if(judge2(i))
				{
					mp1[count1] = i;
					count1++;
					continue;
				}
			}
		}
		if(judge2(n-1) && count1 == 0)
		{
			mp1[count1] = n-1;
			count1++;
		}
		for(int i = 0;i < count2;i+=2)
		strcat(a,s[mp2[i]]);
		for(int i = 0;i < count1;i++)
		strcat(a,s[mp1[i]]);
		for(int i = count2 - 1;i > 0;i-=2)
		strcat(a,s[mp2[i]]);
		printf("%d\n",(count1 + count2) * m);
		printf("%s\n",a);
	}
}


#include<bits/stdc++.h>
using namespace std;

int main()
{int n,m,k,max=0;

cin>>n>>m;

string t[n],s,pal;

for(int i=0;i<n;i++)
cin>>t[i];

for(int i=0;i<n;i++)
{
	s=t[i];
	reverse(s.begin(),s.end());
	if(t[i]==s)
	{
	k=i+1;
	while((k<n)&&(t[k]!=s))
	k++;
	if((k==n)&&((int)t[i].length()>max))
	max=i;
	}
}

if(max!=0)
{
pal=t[max];
t[max]='1';
}

for(int i=0;i<n-1;i++)
{
	if(t[i]!="1")
	{
	s=t[i];
	reverse(s.begin(),s.end());
	for(int j=i+1;j<n;j++)
	{
	if(t[j]==s)
	{
	pal.insert(0,t[i]);
    pal.insert(pal.length(),t[j]);
    t[i]="1";
    t[j]="1";
    s.clear();
    break;
    }
    }
    }
}

//for(int i=0;i<n;i++)
//cout<<t[i];

cout<<pal.length()<<endl<<pal;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{int n,m,k,max=0;

cin>>n>>m;

string t[n],s,pal;

for(int i=0;i<n;i++)
cin>>t[i];

for(int i=0;i<n;i++)
{
	s=t[i];
	reverse(s.begin(),s.end());
	if(t[i]==s)
	{
	k=i+1;
	while((k<n)&&(t[k]!=s))
	k++;
	if((k==n)&&((int)t[i].length()>max))
	max=i;
	}
}

pal=t[max];

for(int i=0;i<n-1;i++)
{
	if(t[i]!="1")
	{
	s=t[i];
	reverse(s.begin(),s.end());
	for(int j=i+1;j<n;j++)
	{
	if(t[j]==s)
	{
	pal.insert(0,t[i]);
    pal.insert(pal.length(),t[j]);
    t[i]="1";
    t[j]="1";
    s.clear();
    break;
    }
    }
    }
}

//for(int i=0;i<n;i++)
//cout<<t[i];

cout<<pal;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{int n,m,k;

cin>>n>>m;

string t[n],s,pal,max="";

for(int i=0;i<n;i++)
cin>>t[i];

for(int i=0;i<n;i++)
{
	s=t[i];
	reverse(s.begin(),s.end());
	if(t[i]==s)
	{
	k=i+1;
	while((k<n)&&(t[k]!=s))
	k++;
	if((k==n)&&((int)t[i].length()>=(int)max.length()))
	{
	max=t[i];
	t[i]='1';
    }
	}
}

if(max!="")
{
pal=max;
}

//for(int i=0;i<n;i++)
//cout<<t[i];

for(int i=0;i<n-1;i++)
{
	if(t[i]!="1")
	{
	s=t[i];
	reverse(s.begin(),s.end());
	for(int j=i+1;j<n;j++)
	{
	if(t[j]==s)
	{
	pal.insert(0,t[i]);
    pal.insert(pal.length(),t[j]);
    t[i]="1";
    t[j]="1";
    s.clear();
    break;
    }
    }
    }
}

//for(int i=0;i<n;i++)
//cout<<t[i];

cout<<pal.length()<<endl<<pal;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{int n,m,k,max=0;

cin>>n>>m;

string t[n],s,pal;

for(int i=0;i<n;i++)
cin>>t[i];

for(int i=0;i<n;i++)
{
	s=t[i];
	reverse(s.begin(),s.end());
	if(t[i]==s)
	{
	k=i+1;
	while((k<n)&&(t[k]!=s))
	k++;
	if((k==n)&&((int)t[i].length()>max))
	max=i;
	}
}

if(max!=0)
{
pal=t[max];
t[max]='1';
}

for(int i=0;i<n-1;i++)
{
	if(t[i]!="1")
	{
	s=t[i];
	reverse(s.begin(),s.end());
	for(int j=i+1;j<n;j++)
	{
	if(t[j]==s)
	{
	pal.insert(0,t[i]);
    pal.insert(pal.length(),t[j]);
    t[i]="1";
    t[j]="1";
    s.clear();
    break;
    }
    }
    }
}

//for(int i=0;i<n;i++)
//cout<<t[i];

cout<<pal;

	return 0;
}


#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int N = 1e5 + 10;

int n, m;
int ff[110][3];
vector<string> S, D;
int main() {
	scanf("%d %d", &n, &m);
	getchar();
	for (int i = 0; i < n; i++) {
		string s, d;
		cin >> s;
		S.push_back(s);
		reverse(s.begin(), s.end());
		D.push_back(s);
	}
	int ans = 0, cnt = 0;
	for (int i = 0; i < n; i++) {
		for (int j = i; j < n; j++) {
			if (S[i]==D[j]) {
				if (i == j) {
					ff[ans][0] = i;
					ff[ans][2] = 1;
					cnt++;
					ans++;
				}
				else {
					ff[ans][0] = i, ff[ans][1] = j;
					ans++;
				}
				break;
			}
		}
	}
	ans -= cnt;
	if (ans == 0) {
		cout << "0" << endl;
		return 0;
	}
	cout << ans*m*2+cnt*m << endl;
	for (int i = 0; i < ans+cnt; i++) {
		if(!ff[i][2])
			cout << S[ff[i][0]];
	}
	for (int i = 0; i < ans+cnt; i++) {
		if (ff[i][2]) {
			cout << S[ff[i][0]];
			break;
		}
	}
	for (int i = ans+cnt - 1; i >= 0; i--) {
		if (!ff[i][2])
			cout << S[ff[i][1]];
	}
}


#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int N = 1e5 + 10;

int n, m;
int ff[110][3];
vector<string> S, D;
int main() {
	scanf("%d %d", &n, &m);
	getchar();
	for (int i = 0; i < n; i++) {
		string s, d;
		cin >> s;
		S.push_back(s);
		reverse(s.begin(), s.end());
		D.push_back(s);
	}
	int ans = 0, cnt = 0;
	for (int i = 0; i < n; i++) {
		for (int j = i; j < n; j++) {
			if (S[i]==D[j]) {
				if (i == j) {
					ff[ans][0] = i;
					ff[ans][2] = 1;
					cnt++;
					ans++;
				}
				else {
					ff[ans][0] = i, ff[ans][1] = j;
					ans++;
				}
				break;
			}
		}
	}
	ans -= cnt;
	if (ans == 0) {
		cout << "0" << endl;
		cout << endl;
		return 0;
	}
	cout << ans*m*2+cnt*m << endl;
	for (int i = 0; i < ans+cnt; i++) {
		if(!ff[i][2])
			cout << S[ff[i][0]];
	}
	for (int i = 0; i < ans+cnt; i++) {
		if (ff[i][2]) {
			cout << S[ff[i][0]];
			break;
		}
	}
	for (int i = ans+cnt - 1; i >= 0; i--) {
		if (!ff[i][2])
			cout << S[ff[i][1]];
	}
}


#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int N = 1e5 + 10;

int n, m;
int ff[110][4];
int flag[110];
vector<string> S, D;
int main() {
	scanf("%d %d", &n, &m);
	getchar();
	for (int i = 0; i < n; i++) {
		string s, d;
		cin >> s;
		S.push_back(s);
		reverse(s.begin(), s.end());
		D.push_back(s);
	}
	int ans = 0, cnt = 0;
	for (int i = 0; i < n; i++) {
		for (int j = i+1; j < n; j++) {
			if (S[i]==D[j]&&!flag[i]&&!flag[j]) {
					ff[ans][0] = i, ff[ans][1] = j;
					flag[i] = 1, flag[j] = 1;
					ans++;
				break;
			}
		}
	}
	for (int i = 0; i < n; i++) {
		if (S[i] == D[i]&&!flag[i]) {
			ff[ans][0] = i;
			ff[ans][2] = 1;
			cnt++;
			ans++;
			break;
		}
	}
	ans -= cnt;
	if (ans == 0) {
		cout << "0" << endl;
		cout << endl;
		return 0;
	}
	cout << ans << " " << cnt << endl;
	cout << ans*m*2+cnt*m << endl;
	for (int i = 0; i < ans+cnt; i++) {
		if(!ff[i][2]&&flag[ff[i][0]])
			cout << S[ff[i][0]];
	}
	for (int i = 0; i < ans+cnt; i++) {
		if (ff[i][2]) {
			cout << S[ff[i][0]];
			break;
		}
	}
	for (int i = ans+cnt - 1; i >= 0; i--) {
		if (!ff[i][2]&&flag[ff[i][1]])
			cout << S[ff[i][1]];
	}
}


#include<bits/stdc++.h>
using namespace std;
const int N = 105;
string s[N], t[N];
int n, m, used[N], p[N];
map<string, vector<int>>mp;
int main()
{
	ios::sync_with_stdio(false);
	cin >> n >> m;
	string ans = "";
	for (int i = 1; i <= n; i++)
	{
		cin >> s[i];
		for (int j = m - 1; j >= 0; j--)
			t[i] += s[i][j];
		p[i] = 1;
		for (int j = 0; j < m; j++)
			if (s[i][j] != s[i][m - j - 1])
				p[i] = 0;
		int k = mp[s[i]].size();
		if (k)
		{
			int r = mp[s[i]][k - 1];
			used[i] = used[r] = 1;
			ans += s[i];
			mp[s[i]].pop_back();
		}
		else
		{
			mp[t[i]].push_back(i);
		}
	}
	string zh = ans;
	for (int i = 1; i <= n; i++)
	{
		if (p[i] && !used[i])
		{
			zh += s[i];
			break;
		}
	}
	for (int i = ans.size() - 1; i >= 0; i--)
		zh += ans[i];
	cout << zh.size() << endl << zh << endl;
	return 0;
}


#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const int N = 1e5 + 10;

int n, m;
int ff[110][4];
int flag[110];
vector<string> S, D;
int main() {
	scanf("%d %d", &n, &m);
	getchar();
	for (int i = 0; i < n; i++) {
		string s, d;
		cin >> s;
		S.push_back(s);
		reverse(s.begin(), s.end());
		D.push_back(s);
	}
	int ans = 0, cnt = 0;
	for (int i = 0; i < n; i++) {
		for (int j = i+1; j < n; j++) {
			if (S[i]==D[j]&&!flag[i]&&!flag[j]) {
					ff[ans][0] = i, ff[ans][1] = j;
					flag[i] = 1, flag[j] = 1;
					ans++;
				break;
			}
		}
	}
	for (int i = 0; i < n; i++) {
		if (S[i] == D[i]&&!flag[i]) {
			ff[ans][0] = i;
			ff[ans][2] = 1;
			cnt++;
			ans++;
			break;
		}
	}
	ans -= cnt;
	if (ans == 0) {
		cout << "0" << endl;
		cout << endl;
		return 0;
	}

	cout << ans*m*2+cnt*m << endl;
	for (int i = 0; i < ans+cnt; i++) {
		if(!ff[i][2]&&flag[ff[i][0]])
			cout << S[ff[i][0]];
	}
	for (int i = 0; i < ans+cnt; i++) {
		if (ff[i][2]) {
			cout << S[ff[i][0]];
			break;
		}
	}
	for (int i = ans+cnt - 1; i >= 0; i--) {
		if (!ff[i][2]&&flag[ff[i][1]])
			cout << S[ff[i][1]];
	}
}