#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll read(){ll ret=0,f=1;char c=getchar();while(c>'9'||c<'0')
{if(c=='-')f=-1;c=getchar();
}while(c>='0'&&c<='9')
{ret=(ret<<1)+(ret<<3)+c-48;c=getchar();
}return ret*f;}
const ll mod=1e9+7;
const int maxn=2e5+7;
int a[maxn];
int pos[maxn];
queue<int>q;
void dfs()
{
    if(q.empty())return;
    int x=q.front();
    q.pop();
    dfs();
    cout <<x<<" ";
}
int main()
{
    int n=read(),k=read();
    int l=0,z=0;
    for(int i=1;i<=n;i++)
    {
        int x=read();
        if(pos[x])continue;
        q.push(x);
        pos[x]=1;
        if(q.size()>k)pos[q.front()]=0,q.pop();

    }
    cout<<q.size()<<endl;
dfs();

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll read(){ll ret=0,f=1;char c=getchar();while(c>'9'||c<'0')
{if(c=='-')f=-1;c=getchar();
}while(c>='0'&&c<='9')
{ret=(ret<<1)+(ret<<3)+c-48;c=getchar();
}return ret*f;}
const ll mod=1e9+7;
const int maxn=2e5+7;
int a[maxn];
unordered_map<int,bool> pos;
int main()
{
    int n=read(),k=read();
    int l=0,z=0;
    for(int i=1;i<=n;i++)
    {
        int x=read();
        a[i]=x;
        if(pos[x])
        {
            a[i]=0;
            z++;
            continue;
        }
        else if(i-z>k)
        {

            while(!a[l])l++;
            pos[a[l]]=0;
            a[l]=0;
            z++;
        }
        pos[x]=1;

    }
    cout<<n-z<<endl;
    for(int i=n;i>=0;i--)
        if(a[i])cout<<a[i]<<" ";

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll read(){ll ret=0,f=1;char c=getchar();while(c>'9'||c<'0')
{if(c=='-')f=-1;c=getchar();
}while(c>='0'&&c<='9')
{ret=(ret<<1)+(ret<<3)+c-48;c=getchar();
}return ret*f;}
const ll mod=1e9+7;
const int maxn=2e5+7;
int a[maxn];
int pos[maxn];
int main()
{
    int n=read(),k=read();
    int l=0,z=0;
    for(int i=1;i<=n;i++)
    {
        int x=read();
        a[i]=x;
        if(pos[x])
        {
            a[i]=0;
            z++;
            continue;
        }
        else if(i-z>k)
        {

            while(!a[l])l++;
            pos[a[l]]=0;
            a[l]=0;
            z++;
        }
        pos[x]=i;

    }
    cout<<n-z<<endl;
    for(int i=n;i>=0;i--)
        if(a[i])cout<<a[i]<<" ";

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll read(){ll ret=0,f=1;char c=getchar();while(c>'9'||c<'0')
{if(c=='-')f=-1;c=getchar();
}while(c>='0'&&c<='9')
{ret=(ret<<1)+(ret<<3)+c-48;c=getchar();
}return ret*f;}
const ll mod=1e9+7;
const int maxn=2e5+7;
int a[maxn];
unordered_map<int,bool> pos;
queue<int>q;
void dfs()
{
    if(q.empty())return;
    int x=q.front();
    q.pop();
    dfs();
    cout <<x<<" ";
}
int main()
{
    int n=read(),k=read();
    int l=0,z=0;
    for(int i=1;i<=n;i++)
    {
        int x=read();
        if(pos[x])continue;
        q.push(x);
        pos[x]=1;
        if(q.size()>k)pos[q.front()]=0,q.pop();

    }
    cout<<q.size()<<endl;
dfs();

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll read(){ll ret=0,f=1;char c=getchar();while(c>'9'||c<'0')
{if(c=='-')f=-1;c=getchar();
}while(c>='0'&&c<='9')
{ret=(ret<<1)+(ret<<3)+c-48;c=getchar();
}return ret*f;}
const ll mod=1e9+7;
const int maxn=2e5+7;
int a[maxn];
map<int,bool> pos;
int main()
{
    int n=read(),k=read();
    int l=0,z=0;
    for(int i=1;i<=n;i++)
    {
        int x=read();
        a[i]=x;
        if(pos[x])
        {
            a[i]=0;
            z++;
            continue;
        }
        else if(i-z>k)
        {

            while(!a[l])l++;
            pos[a[l]]=0;
            a[l]=0;
            z++;
        }
        pos[x]=1;

    }
    cout<<n-z<<endl;
    for(int i=n;i>=0;i--)
        if(a[i])cout<<a[i]<<" ";

}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#define N 70
using namespace std;
typedef long long ll;
const ll mod = 1000000007LL;
int n,k;
int main() {
	cin>>n>>k;
	deque<int> dq;
	dq.clear();
	unordered_set<int> os;
	os.clear();
	for (int i = 0; i < n; ++i) {
		int id;
		cin>>id;
		if (os.find(id) != os.end()) {
			continue;
		} else {
			if (dq.size() == k) {
				os.erase(dq.front());
				dq.pop_front();
			}
			dq.push_back(id);
			os.insert(id);
		}
	}
	cout<<dq.size()<<endl;
	while(!dq.empty()) {
		cout<<dq.back();
		if (dq.size() == 1) cout<<endl;
		else cout<<" ";
		dq.pop_back();
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#define N 70
using namespace std;
typedef long long ll;
const ll mod = 1000000007LL;
int n,k;
int main() {
	cin>>n>>k;
	deque<int> dq;
	dq.clear();
	unordered_set<int> os;
	os.clear();
	for (int i = 0; i < n; ++i) {
		int id;
		scanf("%d", &id);
		if (os.find(id) != os.end()) {
			continue;
		} else {
			if (dq.size() == k) {
				os.erase(dq.front());
				dq.pop_front();
			}
			dq.push_back(id);
			os.insert(id);
		}
	}
	cout<<dq.size()<<endl;
	while(!dq.empty()) {
		//cout<<dq.back();
		printf("%d", dq.back());
		if (dq.size() == 1) printf("\n");
		else printf(" ");
		dq.pop_back();
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#define N 200005
using namespace std;
typedef long long ll;
const ll mod = 1000000007LL;
int n,k;
int ids[N];
int main() {
	cin>>n>>k;
	unordered_set<int> vis;
	vis.clear();
	int b = 0;
	int e = 0;
	for (int i = 0; i < n; ++i) {
		int id;
		scanf("%d", &id);
		if (vis.find(id) != vis.end()) {
			continue;
		} else {
			if (e - b == k) {
				vis.erase(ids[b]);
				b++;
			}
			ids[e++] = id;
			vis.insert(id);
		}
	}
	cout<<e-b<<endl;
	for (int i = e-1; i >= b; --i) {
		printf("%d", ids[i]);
		if (i == b) printf("\n");
		else printf(" ");
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#define N 200005
using namespace std;
typedef long long ll;
const ll mod = 1000000007LL;
int n,k;
int ids[N];
int main() {
	scanf("%d%d",&n,&k);
	unordered_set<int> vis;
	vis.clear();
	int b = 0;
	int e = 0;
	for (int i = 0; i < n; ++i) {
		int id;
		scanf("%d", &id);
		if (vis.find(id) != vis.end()) {
			continue;
		} else {
			if (e - b == k) {
				vis.erase(ids[b]);
				b++;
			}
			ids[e++] = id;
			vis.insert(id);
		}
	}
	printf("%d\n", e-b);
	for (int i = e-1; i >= b; --i) {
		printf("%d", ids[i]);
		if (i == b) printf("\n");
		else printf(" ");
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#define N 200005
using namespace std;
typedef long long ll;
const ll mod = 1000000007LL;
int n,k;
bool isin[N];
int ids[N];
int main() {
	cin>>n>>k;
	memset(isin, false, sizeof(isin));
	int b = 0;
	int e = 0;
	for (int i = 0; i < n; ++i) {
		int id;
		scanf("%d", &id);
		if (isin[id]) {
			continue;
		} else {
			if (e - b == k) {
				isin[ids[b]] = false;
				b++;
			}
			ids[e++] = id;
			isin[ids[e-1]] = true;
		}
	}
	cout<<e-b<<endl;
	for (int i = e-1; i >= b; --i) {
		printf("%d", ids[i]);
		if (i == b) printf("\n");
		else printf(" ");
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#define N 200005
using namespace std;
typedef long long ll;
const ll mod = 1000000007LL;
int n,k;
int ids[N];
int main() {
	scanf("%d%d",&n,&k);
	set<int> vis;
	vis.clear();
	int b = 0;
	int e = 0;
	for (int i = 0; i < n; ++i) {
		int id;
		scanf("%d", &id);
		if (vis.find(id) != vis.end()) {
			continue;
		} else {
			if (e - b == k) {
				vis.erase(ids[b]);
				b++;
			}
			ids[e++] = id;
			vis.insert(id);
		}
	}
	printf("%d\n", e-b);
	for (int i = e-1; i >= b; --i) {
		printf("%d", ids[i]);
		if (i == b) printf("\n");
		else printf(" ");
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
deque<int> dq;
unordered_map<int,int> ump;
inline int read()
{
	char ch=getchar();int ans=0,flag=1;
	while(ch<'0'||ch>'9'){if(ch=='-')flag=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){ans=ans*10+ch-'0';ch=getchar();}
	return ans*flag;
}
int main() {
    int n=read(),k=read();
    while(n--){
        int input=read();
        if(ump[input]!=1) {
            if(dq.size()==k) {
                ump[dq.back()]=0;
                dq.pop_back();
            }
            dq.push_front(input);
            ump[input]=1;
        }
    }
    printf("%d\n",dq.size());
    while(!dq.empty()) {
        printf("%d ",dq.front());
        dq.pop_front();
    }
    // system("pause");
    return 0;
}
/*
7 2
1 2 3 2 1 3 2
*/


#include<bits/stdc++.h>
using namespace std;
deque<int> dq;
map<int,int> ump;
inline int read()
{
	char ch=getchar();int ans=0,flag=1;
	while(ch<'0'||ch>'9'){if(ch=='-')flag=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){ans=ans*10+ch-'0';ch=getchar();}
	return ans*flag;
}
int main() {
    int n=read(),k=read();
    while(n--){
        int input=read();
        if(dq.size()<k){
            if(ump.count(input)==0){
                dq.push_front(input);
                ump[input]=1;
            }
            else {
                if(ump[input]==0){
                    dq.push_front(input);
                    ump[input]=1;
                }
            }
        }
        else {
            if(ump.count(input)==0){
                dq.push_front(input);
                ump[input]=1;
                ump[dq.back()]=0;
                dq.pop_back();
            }
            else {
                if(ump[input]==0) {
                    dq.push_front(input);
                    ump[input]=1;
                    ump[dq.back()]=0;
                    dq.pop_back();
                }
            }
        }
        // if(dq.size()==1)cout<<dq.size()<<"    "<<dq.front()<<endl;
        // else cout<<dq.size()<<"    "<<dq.front()<<" "<<dq.back()<<endl;
    }
    printf("%d\n",dq.size());
    while(!dq.empty()) {
        printf("%d ",dq.front());
        dq.pop_front();
    }
    // system("pause");
    return 0;
}
/*
7 2
1 2 3 2 1 3 2
*/


#include<bits/stdc++.h>
using namespace std;
deque<int> dq;
unordered_map<int,int> ump;
inline int read()
{
	char ch=getchar();int ans=0,flag=1;
	while(ch<'0'||ch>'9'){if(ch=='-')flag=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){ans=ans*10+ch-'0';ch=getchar();}
	return ans*flag;
}
int main() {
    int n=read(),k=read();
    while(n--){
        int input=read();
        if(dq.size()<k){
            if(ump.count(input)==0){
                dq.push_front(input);
                ump[input]=1;
            }
            else {
                if(ump[input]==0){
                    dq.push_front(input);
                    ump[input]=1;
                }
            }
        }
        else {
            if(ump.count(input)==0){
                dq.push_front(input);
                ump[input]=1;
                ump[dq.back()]=0;
                dq.pop_back();
            }
            else {
                if(ump[input]==0) {
                    dq.push_front(input);
                    ump[input]=1;
                    ump[dq.back()]=0;
                    dq.pop_back();
                }
            }
        }
        // if(dq.size()==1)cout<<dq.size()<<"    "<<dq.front()<<endl;
        // else cout<<dq.size()<<"    "<<dq.front()<<" "<<dq.back()<<endl;
    }
    printf("%d\n",dq.size());
    while(!dq.empty()) {
        printf("%d ",dq.front());
        dq.pop_front();
    }
    // system("pause");
    return 0;
}
/*
7 2
1 2 3 2 1 3 2
*/


#include<bits/stdc++.h>
using namespace std;
deque<int> dq;
map<int,int> ump;
inline int read()
{
	char ch=getchar();int ans=0,flag=1;
	while(ch<'0'||ch>'9'){if(ch=='-')flag=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){ans=ans*10+ch-'0';ch=getchar();}
	return ans*flag;
}
int main() {
    int n=read(),k=read();
    while(n--){
        int input=read();
        if(ump[input]!=1) {
            if(dq.size()==k) {
                ump[dq.front()]=0;
                dq.pop_front();
            }
            dq.push_back(input);
            ump[input]=1;
        }
    }
    printf("%d\n",dq.size());
    while(!dq.empty()) {
        printf("%d ",dq.back());
        dq.pop_back();
    }
    // system("pause");
    return 0;
}
/*
7 2
1 2 3 2 1 3 2
*/


#include<bits/stdc++.h>
using namespace std;
deque<int> dq;
unordered_map<int,int> ump;
inline int read()
{
	char ch=getchar();int ans=0,flag=1;
	while(ch<'0'||ch>'9'){if(ch=='-')flag=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){ans=ans*10+ch-'0';ch=getchar();}
	return ans*flag;
}
int main() {
    int n=read(),k=read();
    while(n--){
        int input=read();
        if(ump[input]!=1) {
            if(dq.size()==k) {
                ump[dq.front()]=0;
                dq.pop_front();
            }
            dq.push_back(input);
            ump[input]=1;
        }
    }
    printf("%d\n",dq.size());
    while(!dq.empty()) {
        printf("%d ",dq.back());
        dq.pop_back();
    }
    // system("pause");
    return 0;
}
/*
7 2
1 2 3 2 1 3 2
*/


#include <iostream>
#include <cstdio>
#include <algorithm>
#define K 200010
using namespace std;

struct node{
	int num;
	int index;
}map[K];

bool bo[K];
int a[K*2];

bool cmp(node a,node b){
	return a.num<b.num;
}

int main(){
	int n,k;
	int l=1,r=0;
	for(int i=1;i<=n;i++){
		cin>>map[i].num;
	}
	sort(map+1,map+n+1,cmp);
	map[1].index=1;
	for(int i=2;i<=n;i++){
		if(map[i].num>map[i-1].num) map[i].index=map[i-1].index+1;
	}
    for(int i=1;i<=n;i++){
    	if(bo[map[i].index]==0){
    		bo[map[i].index]=1;
    		if(r-l+1<k){
    			r++;
    			a[r]=map[i].index;
			}
			else{
				r++;
				a[r]=map[i].index;
				bo[a[l]]=0;
				l++;
			}
		}
	}
	cout<<r-l+1;
	for(int i=r;i>=l;i--){
		cout<<map[a[i]].num<<" ";
	}
}


#include <iostream>
#include <cstdio>
#define K 400010
using namespace std;

int map[K];
bool bo[1000000010];

int main(){
	freopen("1.out","r",stdin);
	freopen("2.out","w",stdout);

	int x,n,k,l=1,r=0;

	cin>>n>>k;

    for(int i=1;i<=n;i++){
    	cin>>x;
    	if(bo[x]==0){
		    bo[x]=1;
            if(r-l+1<k){
             	r++;
            	map[r]=x;
	        }
	        else{
	        	r++;
	        	map[r]=x;
				bo[map[l]]=0;
	        	l++;
		   }
    	}
    	else continue;
	}
	cout<<r-l+1<<endl;
	for(int i=r;i>=l;i--){
		cout<<map[i]<<" ";
	}

}


#include <iostream>
#include <cstdio>
#include <algorithm>
#define K 200010
using namespace std;

struct node{
	int num;
	int index;
}map[K];

bool bo[K];
int a[K*2];

bool cmp(node a,node b){
	return a.num<b.num;
}

int main(){

	//freopen("1.out","r",stdin);
	//freopen("2.out","w",stdout);

	int n,k;
	int l=1,r=0;
	for(int i=1;i<=n;i++){
		cin>>map[i].num;
	}
	sort(map+1,map+n+1,cmp);
	map[1].index=1;
	for(int i=2;i<=n;i++){
		if(map[i].num>map[i-1].num) map[i].index=map[i-1].index+1;
	}
    for(int i=1;i<=n;i++){
    	if(bo[map[i].index]==0){
    		bo[map[i].index]=1;
    		if(r-l+1<k){
    			r++;
    			a[r]=map[i].index;
			}
			else{
				r++;
				a[r]=map[i].index;
				bo[a[l]]=0;
				l++;
			}
		}
	}
	cout<<r-l+1;
	for(int i=r;i>=l;i--){
		cout<<map[a[i]].num<<" ";
	}
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#define K 200010
using namespace std;

struct node{
	int num;
	int index;
	int pos;
}map[K];

bool bo[K];
int a[K*2],c[K];

bool cmp1(node a,node b){
	return a.num<b.num;
}
bool cmp2(node a,node b){
	return a.pos<b.pos;
}

int main(){
	int n,k;
	cin>>n>>k;
	int l=1,r=0;
	for(int i=1;i<=n;i++){
		cin>>map[i].num;
		map[i].pos=i;
	}
	sort(map+1,map+n+1,cmp1);
	map[1].index=1;
	c[1]=map[1].num;
	for(int i=2;i<=n;i++){
		if(map[i].num>map[i-1].num){
		    map[i].index=map[i-1].index+1;
		    c[map[i].index]=map[i].num;
		}
		else map[i].index=map[i-1].index;
	}
	sort(map+1,map+n+1,cmp2);

    for(int i=1;i<=n;i++){
    	if(bo[map[i].index]==0){
    		bo[map[i].index]=1;
    		if(r-l+1<k){
    			r++;
    			a[r]=map[i].index;
			}
			else{
				r++;
				a[r]=map[i].index;
				bo[a[l]]=0;
				l++;
			}
		}
	}
	cout<<r-l+1<<endl;
	//sort(map+1,map+n+1,cmp1);
	for(int i=r;i>=l;i--){
		cout<<c[a[i]]<<" ";
	}
}


#include <iostream>
#include <cstdio>
#define K 200010
using namespace std;

int map[K];
bool bo[K]={0};

int main(){
	int n,k;
	cin>>n>>k;
	int x,l=1,r=0;

    for(int i=1;i<=n;i++){
    	cin>>x;
    	if(bo[x]==0){
		    bo[x]=1;
            if(r-l+1<k){
             	r++;
            	map[r]=x;
	        }
	        else{
	        	r++;
	        	map[r]=x;
				bo[map[l]]=0;
	        	l++;

		   }
    	}
	}
	cout<<r-l+1<<endl;
	for(int i=r;i>=l;i--){
		cout<<map[i]<<" ";
	}

}


#include <iostream>
#include <cstdio>
#include <algorithm>
#define K 200010
using namespace std;

struct node{
	int num;
	int index;
}map[K];

bool bo[K];
int a[K*2];

bool cmp(node a,node b){
	return a.num<b.num;
}

int main(){
	//freopen("1.out","r",stdin);
	//freopen("2.out","w",stdout);
	int n,k;
	cin>>n>>k;
	int l=1,r=0;
	for(int i=1;i<=n;i++){
		cin>>map[i].num;
	}
	sort(map+1,map+n+1,cmp);
	map[1].index=1;
	for(int i=2;i<=n;i++){
		if(map[i].num>map[i-1].num) map[i].index=map[i-1].index+1;
	}
    for(int i=1;i<=n;i++){
    	if(bo[map[i].index]==0){
    		bo[map[i].index]=1;
    		if(r-l+1<k){
    			r++;
    			a[r]=map[i].index;
			}
			else{
				r++;
				a[r]=map[i].index;
				bo[a[l]]=0;
				l++;
			}
		}
	}
	cout<<r-l+1;
	for(int i=r;i>=l;i--){
		cout<<map[a[i]].num<<" ";
	}
}


#include <bits/stdc++.h>

using namespace std;

char *p1, *p2, buf[100000];

#define nc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1 ++ )

int rd() {
	int x = 0, f = 1;
	char c = nc();
	while (c < 48) {
		if (c == '-') {
			f = -1;
		}
		c = nc();
	}
	while (c > 47) {
		x = (((x << 2) + x) << 1) + (c ^ 48);
		c = nc();
	}
	return x * f;
}

const int N = 2e5 + 10;

int n, k;

bool vis[N];

int main() {
	n = rd(), k = rd();
	deque <int> q;
	set <int> s;
	for (int i = 1; i <= n; i ++ ) {
		int x = rd();
		if (s.find(x) == s.end()) {
			q.push_front(x);
			s.insert(x);
			if (q.size() > k) {
				int now = q.back();
				s.erase(now);
				q.pop_back();
			}
		}
	}
	printf("%d\n", q.size());
	while (! q.empty()) {
		printf("%d ", q.front());
		q.pop_front();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,k,sum,b[200003],a[200003],t,p;
queue<int>q;
map<int,int>mp;
int main()
{
	cin>>n>>k;
	for(int i=1; i<=n; i++)
	{
		scanf("%d",&a[i]);
		if(mp[a[i]]==0)
			p++,mp[a[i]]=p;
		if(b[mp[a[i]]]==0)
		{
			q.push(a[i]);
			sum++;
			if(sum>k)
			{
				b[mp[q.front()]]=0,sum--;
				q.pop();
			}
		}
		b[mp[a[i]]]=1;
	}
	cout<<sum<<endl;
	while(!q.empty())
	{
		t++;
		b[t]=q.front();
		q.pop();
	}
	for(int i=sum; i>=1; i--)
		cout<<b[i]<<" ";
	return 0;
}


#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>
using namespace std;
vector<int> a;
int now[2000005];
int main()
{
	int n,m;
	cin>>n>>m;
	int wns;
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&wns);
		if(find(a.begin(),a.end(),wns)==a.end())
		{
			a.push_back(wns);
		}
		if(a.size()>m)
		a.erase(a.begin());
    }
	while(a.size()!=0)
	{
		now[++ans]=a[0];
		a.erase(a.begin());
	}
	printf("%d\n",ans);
	for(int i=ans;i>=1;i--)
	{
		printf("%d ",now[i]);
	}
	cout<<endl;
	return 0;
}


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

#define LL long long
#define Debug(x) cerr << #x << ": " << x << endl;

inline int read() {
	char c = getchar(); int x = 0, f = 1;
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -1;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	return x * f;
}

map<int, bool> vis;

int N, M; deque<int> Q;

int main() {
	N = read(); M = read();
	for (int i = 1; i <= N; i++) {
		int x = read();
		if (!vis[x]) {
			if (Q.size() == M) { vis[Q.back()] = false; Q.pop_back(); }
			vis[x] = true; Q.push_front(x);
		}
	}
	printf("%d\n", (int)Q.size());
	for (auto x : Q) printf("%d ", x); printf("\n");
	return 0;
}


//【单调队列优化 DP】
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<deque>
using namespace std;

deque<int> q;
int record[200005];
int a[200005];
int b[200005];
int book[200005]={0};
int record_sum=0;

int Find(int x)
{
	return lower_bound(record+1,record+record_sum+1,x)-record;
}

int main()
{
	int n,k;
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		b[i]=a[i];
	}
	sort(b+1,b+n+1);
	b[0]=-0x3f3f3f3f;
	for(int i=1;i<=n;i++)
		if(b[i]!=b[i-1])
			record[++record_sum]=b[i];
	for(int i=1;i<=n;i++)
	{
		int number=Find(a[i]);
		if(book[number]==1)
			continue;
		book[number]=1;
		if(q.size()<k)
			q.push_front(number);
		else
	    {
	    	book[q.back()]=0;
	    	q.pop_back();
	    	q.push_front(number);
		}
	}
	printf("%d\n",q.size());
	while(q.size()!=0)
	{
		printf("%d ",record[q.front()]);
		q.pop_front();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int ans[200000+10];
queue<int> q;
map<int,int>p;
int main(){
	int k,m,n;
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		int x;
		scanf("%d",&x);
		if(p[x]==1)continue;
		if(q.size()<k){
			q.push(x);
			p[x]=1;
		}
		else{
			int tmp=q.front();
			p[tmp]=0;
			q.pop();
			q.push(x);
			p[x]=1;
		}
	}
	printf("%d\n",q.size());
	int cnt=q.size();
	while(!q.empty()){
		ans[q.size()]=q.front();
		q.pop();
	}
	for(int i=1;i<=cnt;i++)
		printf("%d ",ans[i]);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<vector>
#include<set>
using namespace std;
vector<int>v;
int n,k,x;
int main(){
    scanf("%d%d",&n,&k);
    for(int i=0;i<n;i++){
        scanf("%d",&x);
        vector<int>::iterator it=v.begin();
        for(;it!=v.end();it++)
            if(*it==x) break;
        if(it==v.end()){
            if(v.size()>=k){
                v.erase(v.begin());
                v.push_back(x);
            }
            else v.push_back(x);
        }
    }
    printf("%d\n",v.size());
    for(int i=v.size()-1;i>=0;i--){
        printf("%d",v[i]);
        if(i) printf(" ");
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
const int N=1e9,M=2*1e5;
int n,k,a[M];
bool used[N];
priority_queue<pair<int, int> >q;//用堆优化
int main(){

	scanf("%d %d",&n,&k);
	for(int i=1;i<=n;i++){int x;
	scanf("%d",&x);
	if(!used[x]){
		used[x]=1; q.push(make_pair(1e9-i,x));
		if(q.size()>k){
			int y=q.top().second;q.pop();//出去
			used[y]=false;//更新
		}
	}
	}
	printf("%d\n",q.size());
	int sum=0;
	while(q.size()){
		int y=q.top().second; q.pop();
		sum++; a[sum]=y;
	}
	for(int i=sum;i>=1;i--){
		printf("%d ",a[i]);
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
using namespace std;
typedef long long ll;
const int N=2*1e6+3;
int n,k,cnt,a[N];
queue <int> q;
map<int ,int > my_hash;
int main(){

	q.empty();
	scanf("%d %d",&n,&k);
	for(int i=1;i<=n;i++){
		int x;
		scanf("%d",&x);
		if(!my_hash[x]){
			q.push(x); my_hash[x]=1;
		}
		if(q.size()>k){
			int v=q.front();
			q.pop(); my_hash[v]=0;
		}
	}
	cout<<q.size()<<endl;
	while(!q.empty()){ cnt++;
	a[cnt]=q.front(); q.pop();
	}

	for(int i=cnt;i>=1;i--){
		printf("%d ",a[i]);
	}
	return 0;
}


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

int read()
{
	int xx=0,ff=1;
	char cc=getchar();
	while (cc<48||cc>57)
		ff=cc=='-'?-1:1,cc=getchar();
	while (cc>=48&&cc<=57)
		xx=(xx<<1)+(xx<<3)+(cc^48),cc=getchar();
	return xx*ff;
}

int n,k,m,a[200010];
map<int,bool>mp;
queue<int>q;

int main()
{
	int i,x,y,f=0;
	n=read();
	k=read();
	for (i=1;i<=n;i++)
	{
		x=read();
		if (mp[x]==1)
			continue;/*
		if (f)
		{
			y=q.front();
			q.pop();
			mp[y]=0;
//			mp.insert(make_pair(x,1));
			mp[x]=1;
			q.push(x);
			continue;
		}
		mp.insert(make_pair(x,1));
		q.push(x);
		if (q.size()==k)
			f=1;*/
		if (q.size()==k)
		{
			y=q.front();
			q.pop();
			mp[y]=0;
			q.push(x);
			mp[x]=1;
		}
		else
		{
			q.push(x);
			mp[x]=1;
		}
	}
	printf("%d\n",(m=q.size()));
	for (i=m;i>=1;i--)
	{
		a[i]=q.front();
		q.pop();
	}
	for (i=1;i<=m;i++)
		printf("%d ",a[i]);
	return 0;
}


# include <cstdio>
# include <algorithm>
# include <cstring>
# include <cmath>
# include <climits>
# include <iostream>
//# include <queue>
# include <stack>
# include <string>
# include <set>
# include <map>
# include <cctype>
# include <cstdlib>
# define Rep(i,a,b) for(int i=a;i<=b;i++)
# define _Rep(i,a,b) for(int i=a;i>=b;i--)

using namespace std;

typedef long long ll;

const int N=1e5+5;

template<typename T>void read(T &x){
	x=0;int f=1;
	char c=getchar();
	for(;!isdigit(c);c=getchar())if(c=='-')f=-1;
	for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+c-'0';
	x*=f;
}

int n,k;
deque<int> q;
map<int,bool> mp;

int main()
{
	read(n),read(k);
	Rep(i,1,n){
		int t;
		read(t);
		if(mp[t])continue;
		mp[t]=true;
		q.push_front(t);
		if(q.size()>k){
			int x=q.back();
			mp[x]=false;
			q.pop_back();
		}
	}
	printf("%d\n",q.size());
	while(!q.empty()){
		int x=q.front();
		printf("%d ",x);
		q.pop_front();
	}
	puts("");
	return 0;
}


#include <set>
#include <queue>
#include <iostream>
using namespace std;
const int MAXN = 200005;
int n, k, a[MAXN], c[MAXN];
set <int> ctn;
queue <int> ans;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int csz = 0;
    cin >> n >> k;
    for(int i = 0; i < n; i++) cin >> a[i];
    for(int i = 0; i < n; i++)
        if(!ctn.count(a[i])) {
            ++csz;
            ans.push(a[i]);
            ctn.insert(a[i]);
            if(csz > k) {
                --csz;
                int pos = ans.front(); ans.pop();
                ctn.erase(pos);
            }
        }
    cout << csz << endl;
    for(int i = 0; i < csz; i++) {
        c[i] = ans.front();
        ans.pop();
    }
    for(int i = csz - 1; i >= 0; i--)
        cout << c[i] << ' ';
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define MOD			1000000007
#define FOR(i,l,r)	for(int i=l;i<=r;i++)
#define REP(i,n)	for(int i=1;i<=n;i++)
#define REP0(i,n)	for(int i=0;i<n;i++)
#define PB			push_back
#define MP			make_pair
#define PII			pair<int,int>
#define VI			vector<int>
#define ALL(x)		(x).begin(),(x).end()
int n,k,a[200005];
map<int,int> used;
deque<int> dq;
int main(){
	cin>>n>>k;
	REP(i,n)	cin>>a[i];
	for(int i=1;i<=n;i++){
		if(!used[a[i]]){
			used[a[i]]=1;
			if(dq.size()==k)	used[dq.back()]=0,dq.pop_back();
			dq.push_front(a[i]);
		}
	}
	cout<<dq.size()<<endl;
	while(!dq.empty()){
		cout<<dq.front()<<" ";
		dq.pop_front();
	}
	return 0;
}


#include<bits/stdc++.h>
#include<map>
#include<queue>
#include<stack>
using namespace std;
stack<long> screen3;
map<long,bool> screen2;
queue<long> screen;
int main(){
	long n,k,input;
	scanf("%ld%ld",&n,&k);
	for(int i=1;i<=n;i++){
		scanf("%ld",&input);
		if(not screen2[input]){
			screen.push(input);
			screen2[input]=true;
			if(screen.size()>k){
				screen2[screen.front()]=false;
				screen.pop();
			}
			}
		}
	long num=screen.size();
	printf("%ld\n",num);
	int i=-1;
	while(!screen.empty())
    {
        screen3.push(screen.front());
		screen.pop();
    }
    while(!screen3.empty())
    {
        printf("%ld ",screen3.top());
		screen3.pop();
    }
	return 0;
}


#include<iostream>
using namespace std;
int a[100];
int b[100];
int main()
{
	int n,k,top=1,head,qaq,sum=0;
	bool emmm=true;
	cin>>n>>k;
	head=k;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	for(int i=1;i<=n-head;i++)
	{
		emmm=true;
		for(int j=top;j<=head;j++)
		{
			if(a[j]==a[i]) emmm=false;
		}
		if(emmm==false)
		{
			continue;
		}
		if(emmm==true)
		{
			for(int o=head;o>=top;o--)
			{
				qaq=o;
				a[qaq]=a[qaq-1];
			}
			a[top]=a[i];
		}
	}
	for(int i=head;i>=top;i--)
	{
	    b[a[i]]++;
	    if(b[a[i]]==1)
	    sum++;
	     if(b[a[i]]>1);
	       b[a[i]]=1;
	}
	cout<<sum<<endl;
	for(int i=head;i>=top;i--)
	{

	  if(b[a[i]]==0) continue;
	  cout<<a[i]<<" ";
	  b[a[i]]--;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int v[200010];
map<int,bool>vis;
int main(){
	int n,k,l=1,r=0;
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		int a;
		cin>>a;
		if(vis[a])continue;
		v[++r]=a;
		if(r-l==k)vis[v[l++]]=false;
		vis[a]=true;
	}
	cout<<r-l+1<<"\n";
	for(int i=r;i>=l+1;i--)
	cout<<v[i]<<" ";
	cout<<v[l]<<"\n";
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<deque>
#include<map>
using namespace std;
int n,k,num;
map<int,int>mapp;
deque<int>q;
int main(){
    cin>>n>>k;
    for(int i=1;i<=n;i++){
        cin>>num;
        if(!mapp[num]){
            q.push_front(num);
            mapp[num]++;
        }
        if(q.size()>k){
            mapp[q.back()]=0;
            q.pop_back();
        }
    }
    cout<<q.size()<<'\n';
    while(!q.empty()){
        cout<<q.front()<<" ";
        q.pop_front();
    }
    return 0;
}


#include<iostream>
#include<set>
#include<deque>
using namespace std;
set<int>s;
deque<int>q;
int n,k;
int main(){
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++){
		int x;
		cin>>x;
		if(s.find(x)==s.end()){
			q.push_front(x);
			s.insert(x);
			if(q.size()>k){
				s.erase(q.back());
				q.pop_back();
			}
		}
	}
	cout<<q.size()<<endl;
	while(!q.empty()){
		cout<<q.front()<<" ";
		q.pop_front();
	}
}


#include <bits/stdc++.h>
using namespace std;
map <int,bool> maps;
deque <int> q;
int n,k,t;
int main()
{
	scanf("%d%d",&n,&k);
	for (int i = 1;i <= n; i++)
	{
		scanf("%d",&t);
		if (maps[t] == true) continue;
		maps[t] = true;
		if (q.size() == k)
		{
			int cur = q.front();
			maps[cur] = false;
			q.pop_front();
		}
		q.push_back(t);
	}
	printf("%d\n",q.size());
	while (!q.empty())
	{
		int cur = q.back();
		q.pop_back();
		printf("%d ",cur);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define INF  0x7fffffff//ffffffff
#define N 100010
ll n,k,a[N],x;
queue<int>q;
stack<int>s;
int check(ll x){
	ll flag  =0;
	for(ll i = 0;i <q.size();i++){
		if(q.front()==x)
		flag = 1;
		q.push(q.front());
		q.pop();
	}
	return flag;
}
int main(void){
	cin>>n>>k;
	while(n--){
		cin>>x;
		if(!check(x)){
			q.push(x);
			if(q.size()>k)
			q.pop();
		}
	}
	cout<<q.size()<<endl;
	while(!q.empty()){
		x = q.front();
		q.pop();
		s.push(x);
}
    while(!s.empty()){
    	x = s.top();
    	s.pop();
    	cout<<x<<" ";
	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define INF  0x7fffffff//ffffffff
#define N 1000010
ll n,k,a[N],x,p;
queue<int>q;
stack<int>s;
map<int,int>mp;
int main(void){
	cin>>n>>k;
	while(n--){
		cin>>x;
		if(!mp[x]){
			p++;
			mp[x] = p;
		}
		if(!a[mp[x]]){
			q.push(x);
			if(q.size() >k){
				a[mp[q.front()]] = 0;
				q.pop();
			}
		}
		a[mp[x]] = 1;
	}
	cout<<q.size()<<endl;
	while(!q.empty()){
		x = q.front();
		q.pop();
		s.push(x);
}
    while(!s.empty()){
    	x = s.top();
    	s.pop();
    	cout<<x<<" ";
	}
}


#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;

int main()
{
    vector<int>pho;
    int n,k;
    scanf("%d%d",&n,&k);
    while(n--){

        int a;
        scanf("%d",&a);
        bool is=0;
        int pos;
        for(int i=0;i<pho.size();i++){
            if(pho[i]==a){
                is=1;
                pos=i;
                break;
            }
        }
        if(is)
        {
//            pho.erase(pho.begin()+pos);
//             pho.insert(pho.begin(),a);
            continue;

        }
        else{
            if(pho.size()<k){
                pho.insert(pho.begin(),a);
            }
            else{
                    if(k!=0){
                    pho.pop_back();
                 pho.insert(pho.begin(),a);
                    }

            }
        }

    }

    printf("%d\n",pho.size());
    for(int i=0;i<pho.size();i++){

        printf("%d ",pho[i]);
    }
    printf("\n");
    return 0;
}


#include<iostream>
#include<map>
using namespace std;

int list[200000];
map<int,bool>p;

int main()
{
    int n, k;
    int r = 0;
    int l = 0;
    cin >> n >> k;
    for(int i = 0; i < n; i++)
    {
        int x;
        cin >> x;
        if(!p[x])
        {
            p[x] = true;
            list[r++] = x;
            if(r-l > k)
            {
                p[list[l++]] = false;
            }
        }
    }
    cout << r - 1 << endl;
    while(r > l)
    {
        cout << list[r--] << " ";
    }
    return 0;
}


#include <iostream>
#include <stdio.h>
#include <list>
using namespace std;

int main()
{
    list<int>pho;
    int n,k;
    scanf("%d%d",&n,&k);
    while(n--){

        int a;
        scanf("%d",&a);
        bool is=0;

        list<int>::iterator it;

        for(it=pho.begin();it!=pho.end();it++){
            if(*it==a){
                is=1;

                break;
            }
        }
        if(is)
        {
//            pho.erase(pho.begin()+pos);
//             pho.insert(pho.begin(),a);
            continue;

        }
        else{
            if(pho.size()<k){
                pho.insert(pho.begin(),a);
            }
            else{
                    if(k!=0){
                    pho.pop_back();
                 pho.insert(pho.begin(),a);
                    }

            }
        }

    }

    printf("%d\n",pho.size());
     list<int>::iterator it;
    for(it=pho.begin();it!=pho.end();it++){

        printf("%d ",*it);
    }
    printf("\n");
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int a[N], all[N], st[N], d[N];

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for(int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    all[i] = a[i];
  }
  sort(all + 1, all + 1 + n);
  int m = unique(all + 1, all + 1 + n) - all - 1;
  for(int i = 1; i <= n; ++i) {
    int pos = lower_bound(all + 1, all + 1 + m, a[i]) - all;
    if(!st[pos]) {
      d[++d[0]] = pos;
      st[pos] = d[0];
    } else {
      int tmp = d[0] - st[pos] + 1;
      if(tmp > k) {
        d[++d[0]] = pos;
        st[pos] = d[0];
      }
    }
  }
  int pos = min(k, d[0]);
  printf("%d\n", pos);
  for(int i = d[0]; i >= d[0] - pos + 1; --i)
    printf("%d ", all[d[i]]);
  puts("");
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int a[N], all[N], vis[N], d[N];

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for(int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    all[i] = a[i];
  }
  sort(all + 1, all + 1 + n);
  int m = unique(all + 1, all + 1 + n) - all - 1;
  for(int i = 1; i <= n; ++i) {
    int pos = lower_bound(all + 1, all + 1 + m, a[i]) - all;
    if(!vis[pos]) {
      d[++d[0]] = pos;
    }
    vis[pos] = 1;
  }
  int pos = min(k, d[0]);
  printf("%d\n", pos);
  for(int i = pos; i >= 1; --i)
    printf("%d ", d[i]);
  puts("");
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int a[N], all[N], st[N], d[N];

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for(int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    all[i] = a[i];
  }
  sort(all + 1, all + 1 + n);
  int m = unique(all + 1, all + 1 + n) - all - 1;
  for(int i = 1; i <= n; ++i) {
    int pos = lower_bound(all + 1, all + 1 + m, a[i]) - all;
    if(!st[pos]) {
      d[++d[0]] = pos;
      st[pos] = d[0];
    } else {
      int tmp = d[0] - st[pos] + 1;
      if(tmp > k) {
        d[++d[0]] = pos;
        st[pos] = d[0];
      }
    }
  }
  int pos = min(k, d[0]);
  printf("%d\n", pos);
  for(int i = pos; i >= 1; --i)
    printf("%d ", d[i]);
  puts("");
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int a[N], all[N], st[N], d[N];

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for(int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    all[i] = a[i];
  }
  sort(all + 1, all + 1 + n);
  int m = unique(all + 1, all + 1 + n) - all - 1;
  for(int i = 1; i <= n; ++i) {
    int pos = lower_bound(all + 1, all + 1 + m, a[i]) - all;
    if(!st[pos]) {
      d[++d[0]] = pos;
      st[pos] = d[0];
    } else {
      int tmp = d[0] - st[pos] + 1;
      if(tmp > k) {
        d[++d[0]] = pos;
        st[pos] = d[0];
      }
    }
  }
  int pos = min(k, d[0]);
  printf("%d\n", pos);
  for(int i = d[0]; i >= d[0] - pos + 1; --i)
    printf("%d ", all[d[i]]);
  puts("");
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int a[N], all[N], st[N], d[N];

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for(int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    all[i] = a[i];
  }
  sort(all + 1, all + 1 + n);
  int m = unique(all + 1, all + 1 + n) - all - 1;
  for(int i = 1; i <= n; ++i) {
    int pos = lower_bound(all + 1, all + 1 + m, a[i]) - all;
    if(!st[pos]) {
      d[++d[0]] = pos;
      st[pos] = d[0];
    } else {
      int tmp = d[0] - st[pos] + 1;
      if(tmp > k) {
        d[++d[0]] = pos;
        st[pos] = d[0];
      }
    }
  }
  int pos = min(k, d[0]);
  printf("%d\n", pos);
  for(int i = pos; i >= 1; --i)
    printf("%d ", all[d[i]]);
  puts("");
  return 0;
}


//#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pi pair<int,int>
#define pd pair<ld,ld>
#define ft first
#define sd second
#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define FOR(i,a,b) for(int i=(a); i<=(b);i++)
#define F(i,a,b) FOR(i,(a),(b)-1)
#define REV(i,a,b) for(int i=(a); i>=(b);i--)
#define VI vector<int>
#define VPI vector<pi>
#define VPD vector<pd>
#define PI 3.14159265
#define all(x) (x).begin(), (x).end()
#define sz(a) (int)((a).size())
#define int long long

template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";}
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
	while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...);
}
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
mt19937 r(chrono::steady_clock::now().time_since_epoch().count());
int rand_range(int a, int b)
{
	assert(a<=b);
	return a+r()%(b-a+1);
}
const long long K = 1000;
const long long KK = K*K;
const long long MIL = KK*K;
const long long INF = MIL*MIL;
const long long MOD = MIL+7;
const long long N = 1e5+10;
int dp[N][30];

void solve(){
    int n;
    cin>>n;
    FOR(i,1,n){
        string s;
        cin>>s;
        bool identyczne = true;
        F(j,1,sz(s))
            if(s[j] != s[0])
                identyczne = false;
        F(j,0,26)
            dp[i][j] = (dp[i-1][j] != 0);
        if(identyczne){
            int a = s[0]-'a';
            dp[i][a]=(dp[i-1][a]+1)*sz(s)+dp[i-1][a];
        }else{
            int pre=0;
            F(j,0,sz(s)){
                if(s[j]==s[0])
                    pre++;
                else
                    break;
            }
            dp[i][s[0]-'a']=pre+(dp[i-1][s[0]-'a'] != 0);
            int suf = 0;
            REV(j,sz(s)-1,0){
                if(s[j]==s.back())
                    suf++;
                else
                    break;
            }
            dp[i][ s.back()-'a' ] = suf+ (dp[i-1][s.back()-'a'] != 0);
            pi pom(s[0],0);
            F(j,0,sz(s)){
                if(s[j]==pom.ft)
                    pom.sd++;
                else{
                    dp[i][pom.ft-'a']=max(dp[i][pom.ft-'a'], pom.sd);
                    pom = pi(s[j], 1);
                }
            }
            dp[i][pom.ft-'a']=max(dp[i][pom.ft-'a'], pom.sd);
        }
    }
    int maxi = 0;
    F(i,0,30){
        maxi = max(maxi, dp[n][i]);
//        debug(i, dp[n][i]);
    }

    cout<<maxi<<"\n";
}

int32_t main()
{
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);

	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cerr.tie(0);
	cout<<setprecision(9)<<fixed;
	cerr<<setprecision(6)<<fixed;
	int test = 1;
//    cin>>test;
	while(test--){
		solve();
	}
}
/*
1 2 5
1 1 0 5000
9 0 1 7000

*/


//#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pi pair<int,int>
#define pd pair<ld,ld>
#define ft first
#define sd second
#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define FOR(i,a,b) for(int i=(a); i<=(b);i++)
#define F(i,a,b) FOR(i,(a),(b)-1)
#define REV(i,a,b) for(int i=(a); i>=(b);i--)
#define VI vector<int>
#define VPI vector<pi>
#define VPD vector<pd>
#define PI 3.14159265
#define all(x) (x).begin(), (x).end()
#define sz(a) (int)((a).size())
#define int long long

template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";}
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
	while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...);
}
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
mt19937 r(chrono::steady_clock::now().time_since_epoch().count());
int rand_range(int a, int b)
{
	assert(a<=b);
	return a+r()%(b-a+1);
}
const long long K = 1000;
const long long KK = K*K;
const long long MIL = KK*K;
const long long INF = MIL*MIL;
const long long MOD = MIL+7;
const long long N = 1e5+10;
int dp[N][30];

void solve(){
    int n;
    cin>>n;
    FOR(i,1,n){
        string s;
        cin>>s;
        bool identyczne = true;
        F(j,1,sz(s))
            if(s[j] != s[0])
                identyczne = false;
        F(j,0,26)
            dp[i][j] = (dp[i-1][j] != 0);
        if(identyczne){
            int a = s[0]-'a';
            dp[i][a]=(dp[i-1][a]+1)*sz(s)+dp[i-1][a];
        }else{
            int pre=0;
            F(j,0,sz(s)){
                if(s[j]==s[0])
                    pre++;
                else
                    break;
            }
            dp[i][s[0]-'a']=max(dp[i][s[0]-'a'], pre+(dp[i-1][s[0]-'a'] != 0));
            int suf = 0;
            REV(j,sz(s)-1,0){
                if(s[j]==s.back())
                    suf++;
                else
                    break;
            }
            dp[i][ s.back()-'a' ] =max(dp[i][s.back()-'a'], suf+ (dp[i-1][s.back()-'a'] != 0));
            if(s[0] == s.back() && dp[i-1][s[0]-'a']){
                dp[i][s[0] -'a'] = max(dp[i][s[0] -'a'], pre + 1 + suf);
            }
            pi pom(s[0],0);
            F(j,0,sz(s)){
                if(s[j]==pom.ft)
                    pom.sd++;
                else{
                    dp[i][pom.ft-'a']=max(dp[i][pom.ft-'a'], pom.sd);
                    pom = pi(s[j], 1);
                }
            }
            dp[i][pom.ft-'a']=max(dp[i][pom.ft-'a'], pom.sd);
        }
        F(j,0,30)
            dp[i][j]=min(MIL, dp[i][j]);
    }
    int maxi = 0;
    F(i,0,30){
        maxi = max(maxi, dp[n][i]);
//        debug(i, dp[n][i]);
    }

    cout<<maxi<<"\n";
}

int32_t main()
{
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);

	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cerr.tie(0);
	cout<<setprecision(9)<<fixed;
	cerr<<setprecision(6)<<fixed;
	int test = 1;
//    cin>>test;
	while(test--){
		solve();
	}
}
/*
1 2 5
1 1 0 5000
9 0 1 7000

*/


#include "bits/stdc++.h"

using namespace std;

// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //

#define int                                       long long
#define lop(i,s,e)                                for(int i=s;i<(int)e;i++)
#define rlop(i,e,s)                               for(int i=e;i>=s;i--)
#define prvec(v)                                  lop(R15,0,v.size())cout << v[R15] <<" "; cout << endl;
#define prarr(arr,s1,e1)                          lop(R15,s1,e1)cout << arr[R15] << " "; cout << endl;

#define endl                                      '\n'
#define ff                                        first
#define ss                                        second
#define pii                                       pair<int,int>

#define pb                                        push_back
#define vi                                        vector<int>
#define vpii                                      vector< pii >

#define mii                                       map< int, int >
#define msi                                       map< string, int >
#define mib                                       map< int, bool >

#define all(v)                                    v.begin(),v.end()
#define mset(a,f)                                 memset(a,f,sizeof(a))
#define M                                         1000000007
#define Think_Twice_Code_Once                     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)

#define bug(...)                                  __f (#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }
template <typename Arg1, typename... Args>
void __f (const char* names, Arg1&& arg1, Args&&... args)
{
     const char* comma = strchr (names + 1, ',');
     cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...);
}

// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //

inline void INP();
const int N = 3e6 + 7;
const long long INF = LLONG_MAX;
const int inf = INT_MAX;

void get(string &str, vi &v1)
{
	int cnt = 1, n1 = str.length();
	char ch = str[0];

	lop(i,1,n1+1)
	{
		if( i < n1 and str[i] == str[i-1] ) cnt++;
		else
		{
			int val = ch - 'a';
			v1[ val ] = max( v1[val], cnt );
			cnt = 1;
			if( i < n1 ) ch = str[i];
		}
	}

	int p = 1, i = 1;
	while( i < n1 and str[i] == str[i-1] )p++, i++;

	i  = n1 - 2 ;
	int s = 1;
	while( i >= 0 and str[i] == str[i+1] )s++, i--;

	v1[26] = p;
	v1[27] = s;

}
void solve()
{
	int n;
      cin >> n;
      string str[n];
      lop(i,0,n) cin >> str[i];

      vector< vi > vec(n, vi (28,0));
      vi ans(28,0);

      lop(i,0,n) get(str[i], vec[i]);
      ans = vec[0];

      lop(i, 1, n)
      {
      	int n1 = str[i].length();

      	if( vec[i][26] != n1 )
      	{
	      	lop(j,0,26)
	      	{
	      		char ch = j + 'a';
	      		if( ans[j] == 0 ) ans[j] = vec[i][j];
	      		else if( ch != str[i][0] and ch != str[i][n1-1] ) ans[j] = max(1LL, vec[i][j]);
	      		else if( ch == str[i][0] and ch == str[i][n1-1] )
	      		{
	      			int u  = 1 + vec[i][26] + vec[i][27];
	      			ans[j] = max(vec[i][j], u);
	      		}
	      		else if( ch == str[i][0] ) ans[j] = max( vec[i][j], 1 + vec[i][26] );
	      		else ans[j] = max( vec[i][j], 1 + vec[i][27]);
	      	}
      	}
      	else
      	{
      		lop(j,0,26)
      		{
      			char ch = j + 'a';
      			if( ans[j] == 0 ) ans[j] = vec[i][j];
      			else if( ch != str[i][0] ) ans[j] = 1;
      			else ans[j] = ans[j] + (ans[j]+1)*vec[i][j];
      		}
      	}
      }

      int res = 0;
      lop(i,0,26) res = max( res, ans[i] );
      cout << res;
}

int32_t main()
{

      Think_Twice_Code_Once;
      INP();
      int t = 1;
      //cin >> t;
      while(t--) solve();

}

inline void INP()
{
      #ifndef ONLINE_JUDGE
          freopen("input.txt",  "r",  stdin);
          freopen("output.txt", "w", stdout);
      #endif
}

// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //


#include "bits/stdc++.h"

using namespace std;

// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //

#define int                                       long long
#define lop(i,s,e)                                for(int i=s;i<(int)e;i++)
#define rlop(i,e,s)                               for(int i=e;i>=s;i--)
#define prvec(v)                                  lop(R15,0,v.size())cout << v[R15] <<" "; cout << endl;
#define prarr(arr,s1,e1)                          lop(R15,s1,e1)cout << arr[R15] << " "; cout << endl;

#define endl                                      '\n'
#define ff                                        first
#define ss                                        second
#define pii                                       pair<int,int>

#define pb                                        push_back
#define vi                                        vector<int>
#define vpii                                      vector< pii >

#define mii                                       map< int, int >
#define msi                                       map< string, int >
#define mib                                       map< int, bool >

#define all(v)                                    v.begin(),v.end()
#define mset(a,f)                                 memset(a,f,sizeof(a))
#define M                                         1000000007
#define Think_Twice_Code_Once                     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)

#define bug(...)                                  __f (#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }
template <typename Arg1, typename... Args>
void __f (const char* names, Arg1&& arg1, Args&&... args)
{
     const char* comma = strchr (names + 1, ',');
     cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...);
}

// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //

inline void INP();
const int N = 3e6 + 7;
const long long INF = LLONG_MAX;
const int inf = INT_MAX;

int n, Q;
int arr[N];

void get(string &str, vi &v1)
{
	int cnt = 1, n1 = str.length();
	char ch = str[0];

	lop(i,1,n1+1)
	{
		if( i < n1 and str[i] == str[i-1] ) cnt++;
		else
		{
			int val = ch - 'a';
			v1[ val ] = max( v1[val], cnt );
			cnt = 1;
			if( i < n1 ) ch = str[i];
		}
	}

	int p = 1, i = 1;
	while( i < n1 and str[i] == str[i-1] )p++, i++;

	i  = n1 - 2 ;
	int s = 1;
	while( i >= 0 and str[i] == str[i+1] )s++, i--;

	v1[26] = p;
	v1[27] = s;

}
void solve()
{
      cin >> n;
      string str[n];
      lop(i,0,n) cin >> str[i];

      vector< vi > vec(n, vi (28,0));
      vi ans(28,0);

      lop(i,0,n) get(str[i], vec[i]);
      ans = vec[0];

      lop(i, 1, n)
      {
      	int n1 = str[i].length();

      	if( vec[i][26] != n1 )
      	{
	      	lop(j,0,26)
	      	{
	      		char ch = j + 'a';
	      		if( ans[j] == 0 ) ans[j] = vec[i][j];
	      		else if( ch != str[i][0] and ch != str[i][n1-1] ) ans[j] = vec[i][j];
	      		else if( ch == str[i][0] and ch == str[i][n1-1] )
	      		{
	      			ans[j] = 1 + vec[i][26] + vec[i][27];
	      		}
	      		else if( ch == str[i][0] ) ans[j] = 1 + vec[i][26];
	      		else ans[j] = 1 + vec[i][27];
	      	}
      	}
      	else
      	{
      		lop(j,0,26)
      		{
      			char ch = j + 'a';
      			if( ans[j] == 0 ) ans[j] = vec[i][j];
      			else if( ch != str[i][0] ) ans[j] = 1;
      			else ans[j] = ans[j] + (ans[j]+1)*vec[i][j];
      		}
      	}
      }

      int res = 0;
      lop(i,0,26) res = max( res, ans[i] );
      cout << res;
}

int32_t main()
{

      Think_Twice_Code_Once;
      INP();
      int t = 1;
      //cin >> t;
      while(t--) solve();

}

inline void INP()
{
      #ifndef ONLINE_JUDGE
          freopen("input.txt",  "r",  stdin);
          freopen("output.txt", "w", stdout);
      #endif
}

// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //


#include "bits/stdc++.h"

using namespace std;

// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //

#define int                                       long long
#define lop(i,s,e)                                for(int i=s;i<(int)e;i++)
#define rlop(i,e,s)                               for(int i=e;i>=s;i--)
#define prvec(v)                                  lop(R15,0,v.size())cout << v[R15] <<" "; cout << endl;
#define prarr(arr,s1,e1)                          lop(R15,s1,e1)cout << arr[R15] << " "; cout << endl;

#define endl                                      '\n'
#define ff                                        first
#define ss                                        second
#define pii                                       pair<int,int>

#define pb                                        push_back
#define vi                                        vector<int>
#define vpii                                      vector< pii >

#define mii                                       map< int, int >
#define msi                                       map< string, int >
#define mib                                       map< int, bool >

#define all(v)                                    v.begin(),v.end()
#define mset(a,f)                                 memset(a,f,sizeof(a))
#define M                                         1000000007
#define Think_Twice_Code_Once                     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)

#define bug(...)                                  __f (#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }
template <typename Arg1, typename... Args>
void __f (const char* names, Arg1&& arg1, Args&&... args)
{
     const char* comma = strchr (names + 1, ',');
     cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...);
}

// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //

inline void INP();
const int N = 3e6 + 7;
const long long INF = LLONG_MAX;
const int inf = INT_MAX;

int n, Q;
int arr[N];

void get(string &str, vi &v1)
{
	int cnt = 1, n1 = str.length();
	char ch = str[0];

	lop(i,1,n1+1)
	{
		if( i < n1 and str[i] == str[i-1] ) cnt++;
		else
		{
			int val = ch - 'a';
			v1[ val ] = max( v1[val], cnt );
			cnt = 1;
			if( i < n1 ) ch = str[i];
		}
	}

	int p = 1, i = 1;
	while( i < n1 and str[i] == str[i-1] )p++, i++;

	i  = n1 - 2 ;
	int s = 1;
	while( i >= 0 and str[i] == str[i+1] )s++, i--;

	v1[26] = p;
	v1[27] = s;

}
void solve()
{
      cin >> n;
      string str[n];
      lop(i,0,n) cin >> str[i];

      vector< vi > vec(n, vi (28,0));
      vi ans(28,0);

      lop(i,0,n) get(str[i], vec[i]);
      ans = vec[0];

      lop(i, 1, n)
      {
      	int n1 = str[i].length();

      	if( vec[i][26] != n1 )
      	{
	      	lop(j,0,26)
	      	{
	      		char ch = j + 'a';
	      		if( ans[j] == 0 ) ans[j] = vec[i][j];
	      		else if( ch != str[i][0] and ch != str[i][n1-1] ) ans[j] = vec[i][j];
	      		else if( ch == str[i][0] and ch == str[i][n1-1] )
	      		{
	      			int u  = 1 + vec[i][26] + vec[i][27];
	      			ans[j] = max(vec[i][j], u);
	      		}
	      		else if( ch == str[i][0] ) ans[j] = max( vec[i][j], 1 + vec[i][26] );
	      		else ans[j] = max( vec[i][j], 1 + vec[i][27]);
	      	}
      	}
      	else
      	{
      		lop(j,0,26)
      		{
      			char ch = j + 'a';
      			if( ans[j] == 0 ) ans[j] = vec[i][j];
      			else if( ch != str[i][0] ) ans[j] = 1;
      			else ans[j] = ans[j] + (ans[j]+1)*vec[i][j];
      		}
      	}
      }

      int res = 0;
      lop(i,0,26) res = max( res, ans[i] );
      cout << res;
}

int32_t main()
{

      Think_Twice_Code_Once;
      INP();
      int t = 1;
      //cin >> t;
      while(t--) solve();

}

inline void INP()
{
      #ifndef ONLINE_JUDGE
          freopen("input.txt",  "r",  stdin);
          freopen("output.txt", "w", stdout);
      #endif
}

// M A F I A - M A F I A - M A F I A - M A F I A - M A F I A - M A F I A //


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

void print(int a[26]){
	for(int i=0; i<26; i++){
		cout<<a[i]<<" ";
	}
	cout<<"\n";
}
void getFrequency(int a[26],string s){
	//todo change to get max
	char curr=s[0];
	int count=1;
	for(int i=1; i<s.length(); i++){
		if(s[i]!=curr){
			a[curr-'a'] = max(count,a[curr-'a']);
			curr=s[i];
			count=1;
		}else{
			count++;
		}
	}
	a[curr-'a'] = max(count,a[curr-'a']);
}

void calculate(string s,int a[26]){
	int N=s.length();

	char first=s[0];
	char last=s[s.length()-1];

	int fCount=0;
	for(int i=0; i<N; i++){
		if(s[i]!=first)
			break;
		fCount++;
	}

	int lCount=0;
	for(int i=N-1; i>=0; i--){
		if(s[i]!=last)
			break;
		lCount++;
	}

	int b[26]={0};
	getFrequency(b,s);

	if(first==last){
		int currCount=a[first-'a'];
		if(fCount==N){
			b[first-'a']= (N+1)*currCount+N;
		}
		else{
			if(currCount>0)
				b[first-'a']= max(b[first-'a'],(fCount+ 1 + lCount));
		}

	}else{
		for(int i=0; i<26; i++){
			if(first -'a' == i){
				if(a[i]>0)
					b[i] = max(fCount+1, b[i]);
			}else if(last -'a' == i){
				if(a[i]>0)
					b[i] = max(lCount+1, b[i]);
			}
		}
	}
	for(int i=0; i<26; i++){
		if(b[i]==0){
			a[i]=min(a[i],1);
		}else{
			a[i]=b[i];
		}
	}

}

int main() {

	int n;
	cin>>n;

	int a[26]={0};
	string temp;
	cin>>temp;

	getFrequency(a,temp);
//	print(a);

	for(int i=1; i<n; i++){
		cin>>temp;
		calculate(temp,a);
		//print(a);
	}

	int max=0;
	for(int i=0; i<26; i++){
		if(max<a[i])
			max=a[i];
	}
	cout<<max;

	return 0;
}


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

typedef long long int lli;

void print(lli a[26]){
	for(int i=0; i<26; i++){
		cout<<a[i]<<" ";
	}
	cout<<"\n";
}
void getFrequency(lli a[26],string s){
	//todo change to get max
	char curr=s[0];
	int count=1;
	for(int i=1; i<s.length(); i++){
		if(s[i]!=curr){
			a[curr-'a'] = max(count,a[curr-'a']);
			curr=s[i];
			count=1;
		}else{
			count++;
		}
	}
	a[curr-'a'] = max(count,a[curr-'a']);
}

void calculate(string s,lli a[26]){
	int N=s.length();

	char first=s[0];
	char last=s[s.length()-1];

	int fCount=0;
	for(int i=0; i<N; i++){
		if(s[i]!=first)
			break;
		fCount++;
	}

	int lCount=0;
	for(int i=N-1; i>=0; i--){
		if(s[i]!=last)
			break;
		lCount++;
	}

	lli b[26]={0};
	getFrequency(b,s);

	if(first==last){
		int currCount=a[first-'a'];
		if(fCount==N){
			b[first-'a']= (N+1)*currCount+N;
		}
		else{
			if(currCount>0)
				b[first-'a']= max(b[first-'a'],(fCount+ 1 + lCount));
		}

	}else{
		for(int i=0; i<26; i++){
			if(first -'a' == i){
				if(a[i]>0)
					b[i] = max(fCount+1, b[i]);
			}else if(last -'a' == i){
				if(a[i]>0)
					b[i] = max(lCount+1, b[i]);
			}
		}
	}
	for(int i=0; i<26; i++){
		if(b[i]==0){
			a[i]=min(a[i],1);
		}else{
			a[i]=b[i];
		}
	}

}

int main() {

	int n;
	cin>>n;

	lli a[26]={0};
	string temp;
	cin>>temp;

	getFrequency(a,temp);
//	print(a);

	for(int i=1; i<n; i++){
		cin>>temp;
		calculate(temp,a);
		//print(a);
	}

	int max=0;
	for(int i=0; i<26; i++){
		if(max<a[i])
			max=a[i];
	}
	cout<<max;

	return 0;
}


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

typedef long long int lli;

void print(lli a[26]){
	for(int i=0; i<26; i++){
		cout<<a[i]<<" ";
	}
	cout<<"\n";
}
void getFrequency(lli a[26],string s){
	//todo change to get max
	char curr=s[0];
	lli count=1;
	for(int i=1; i<s.length(); i++){
		if(s[i]!=curr){
			a[curr-'a'] = max(count,a[curr-'a']);
			curr=s[i];
			count=1;
		}else{
			count++;
		}
	}
	a[curr-'a'] = max(count,a[curr-'a']);
}

void calculate(string s,lli a[26]){
	int N=s.length();

	char first=s[0];
	char last=s[s.length()-1];

	lli fCount=0;
	for(int i=0; i<N; i++){
		if(s[i]!=first)
			break;
		fCount++;
	}

	lli lCount=0;
	for(int i=N-1; i>=0; i--){
		if(s[i]!=last)
			break;
		lCount++;
	}

	lli b[26]={0};
	getFrequency(b,s);

	if(first==last){
		int currCount=a[first-'a'];
		if(fCount==N){
			b[first-'a']= (N+1)*currCount+N;
		}
		else{
			if(currCount>0)
				b[first-'a']= max(b[first-'a'],(fCount+ 1 + lCount));
		}

	}else{
		for(int i=0; i<26; i++){
			if(first -'a' == i){
				if(a[i]>0)
					b[i] = max(fCount+1, b[i]);
			}else if(last -'a' == i){
				if(a[i]>0)
					b[i] = max(lCount+1, b[i]);
			}
		}
	}
	for(int i=0; i<26; i++){
		if(b[i]==0){
			a[i]=(a[i]==0)? 0:1;
		}else{
			a[i]=b[i];
		}
	}

}

int main() {

	int n;
	cin>>n;

	lli a[26]={0};
	string temp;
	cin>>temp;

	getFrequency(a,temp);
//	print(a);

	for(int i=1; i<n; i++){
		cin>>temp;
		calculate(temp,a);
		//print(a);
	}

	int max=0;
	for(int i=0; i<26; i++){
		if(max<a[i])
			max=a[i];
	}
	cout<<max;

	return 0;
}


#include <bits/stdc++.h>
#define sz(v)   ((int)(v).size())
#define  all(v)    ((v).begin()),((v).end())
#define  allr(v)    ((v).rbegin()),((v).rend())
#define   pb         push_back
#define   mp         make_pair
#define   mt         make_tuple
//#define   Y            imag()
//#define   X            real()
#define    clr(v,d)      memset( v, d ,sizeof(v))
#define   angle(n)      atan2((n.imag()),(n.real()))
#define   vec(a,b)       ((b)-(a))
#define   length(a)      hypot( (a.imag()),(a.real()) )
#define   normalize(a)      (a)/(length(a))
//#define    dp(a,b)          (((conj(a))*(b)).real())
//#define    cp(a,b)          (((conj(a))*(b)).imag())
#define    lengthsqrt(a)       dp(a,a)
#define    rotate0( a,ang)    ((a)*exp( point(0,ang) ))
#define    rotateA(about,p,ang)   (rotate0(vec(about,p),ang)+about)
#define    lcm(a,b)                ((a*b)/(__gcd(a,b)))
#define    reflection0(m,v)         (conj((v)/(m))*(m))
#define     reflectionA(m,v,p0)     (conj( (vec(p0,v))/(vec(p0,m)) ) * (vec(p0,m)) ) + p0
//#define     same(p1,p2)               ( dp(  vec(p1,p2),vec(p1,p2)) < eps )
#define     PT                    complex<double>
#define outfile freopen("out.out", "w", stdout);
#define infile  freopen("in.in", "r", stdin);
#define PI acos(-1)
typedef  long long     ll ;
typedef  unsigned long long ull;
const double eps= (1e-9);
using namespace std;
int dcmp(long double a,long double b){   return fabsl(a-b)<=eps ? 0: (a>b)? 1:-1  ;}
int getBit(int num, int idx) {return ((num >> idx) & 1) == 1;}
int setBit1(int num, int idx) {return num | (1<<idx);}
ll setBit0(ll num, int idx) {return num & ~(1ll<<idx);}
ll flipBit(int num, int idx) {return num ^ (1<<idx);}
void FS(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);}
int countNumBit1(int mask) {int ret=0; while (mask)     {mask &= (mask-1);++ret;    }return ret;}
const int N=100009;
string s[N];
int n,vis[27];
int longest(string &s,char c)
{
    int mx=0,cnt=0;
    for(int i=0;i<sz(s);i++)
    {
        if(s[i]==c)
            cnt++;
        else
            mx=max(mx,cnt),cnt=0;
    }
    mx=max(mx,cnt);
    return mx;
}
int getP(string &s,char c)
{
    int cnt=0;
    for(int i=0;i<sz(s);i++)
    {
        if(s[i]==c)
            cnt++;
        else
            break;
    }
    return cnt;
}
int getS(string &s,char c)
{
    int cnt=0;
    for(int i=sz(s)-1;i>=0;i--)
    {
        if(s[i]==c)
            cnt++;
        else
            break;
    }
    return cnt;
}
int solve(int n,char c)
{
    int res= longest(s[1],c);
    for(int i=2;i<=n;i++)
    {
        int lgs=longest(s[i],c);
        int prefix=longest(s[i],c);
        int suffix=longest(s[i],c);

        if(lgs==sz(s[i]))
        {
            res= res+lgs*(res+1);
        }
        else if(res>0)
        {
            res= max(prefix+suffix+1,lgs);
        }
        else
        {
            res=lgs;
        }
    }
    return res;
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>s[i];
        for(int j=0;j<sz(s[i]);j++)
        {
            vis[s[i][j]-'a']++;
        }
    }
    int ans=1;
    for(int i=0;i<26;i++)
        if(vis[i]>0)
        ans=max(ans,solve(n,i+'a'));
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define sz(v)   ((int)(v).size())
#define  all(v)    ((v).begin()),((v).end())
#define  allr(v)    ((v).rbegin()),((v).rend())
#define   pb         push_back
#define   mp         make_pair
#define   mt         make_tuple
//#define   Y            imag()
//#define   X            real()
#define    clr(v,d)      memset( v, d ,sizeof(v))
#define   angle(n)      atan2((n.imag()),(n.real()))
#define   vec(a,b)       ((b)-(a))
#define   length(a)      hypot( (a.imag()),(a.real()) )
#define   normalize(a)      (a)/(length(a))
//#define    dp(a,b)          (((conj(a))*(b)).real())
//#define    cp(a,b)          (((conj(a))*(b)).imag())
#define    lengthsqrt(a)       dp(a,a)
#define    rotate0( a,ang)    ((a)*exp( point(0,ang) ))
#define    rotateA(about,p,ang)   (rotate0(vec(about,p),ang)+about)
#define    lcm(a,b)                ((a*b)/(__gcd(a,b)))
#define    reflection0(m,v)         (conj((v)/(m))*(m))
#define     reflectionA(m,v,p0)     (conj( (vec(p0,v))/(vec(p0,m)) ) * (vec(p0,m)) ) + p0
//#define     same(p1,p2)               ( dp(  vec(p1,p2),vec(p1,p2)) < eps )
#define     PT                    complex<double>
#define outfile freopen("out.out", "w", stdout);
#define infile  freopen("in.in", "r", stdin);
#define PI acos(-1)
typedef  long long     ll ;
typedef  unsigned long long ull;
const double eps= (1e-9);
using namespace std;
int dcmp(long double a,long double b){   return fabsl(a-b)<=eps ? 0: (a>b)? 1:-1  ;}
int getBit(int num, int idx) {return ((num >> idx) & 1) == 1;}
int setBit1(int num, int idx) {return num | (1<<idx);}
ll setBit0(ll num, int idx) {return num & ~(1ll<<idx);}
ll flipBit(int num, int idx) {return num ^ (1<<idx);}
void FS(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);}
int countNumBit1(int mask) {int ret=0; while (mask)     {mask &= (mask-1);++ret;    }return ret;}
int n;
const int lim=1000000000;
vector< string > s;
int getmax(string st)
{
    int mx=0,cnt=0;
    for(int i=0;i<sz(st);i++)
    {
        if(i+1<sz(st)&&st[i]==st[i+1])
        {
            cnt++;
        }
        else
        {
            cnt++;
            mx=max(mx,cnt);
            cnt=0;
        }
    }
    mx=max(cnt,mx);
    return mx;
}
int getcharmax(string st,char ch)
{
    int mx=0,cnt=0;
    for(int i=0;i<sz(st);i++)
    {
        if(st[i]==ch)
        {
            cnt++;
        }
        else
        {
            mx=max(mx,cnt);
            cnt=0;
        }
    }
    mx=max(cnt,mx);
    return mx;
}
pair<int,char> getprefix(string st)
{
    int cnt=0;
    for(int i=0;i<sz(st);i++)
    {
        if(i+1<sz(st)&&st[i]!=st[i+1])
        {
            cnt++;
            break;
        }
        else
        {
            cnt++;
        }
    }
    return mp(cnt,st[0]);
}
pair<bool,char> isonechar(string st)
{
    char ch=st[0];
    int n=sz(st);
    for(int i=0;i<n;i++)
    {
        if(st[i]!=ch)
            return mp(0,'a');
    }
    return mp(1,ch);
}
pair<int,char> getsuffix(string st)
{
    int cnt=0;
    int n=sz(st);
    for(int i=n-1;i>=0;i--)
    {
        if(i-1>=0&&st[i]!=st[i-1])
        {
            cnt++;
            break;
        }
        else
        {
            cnt++;
        }
    }
    return mp(cnt,st[n-1]);
}
bool isfind(int l,int r,char ch)
{
    for(int i=l;i<=r;i++)
    {
        for(int j=0;j<sz(s[i]);j++)
        {
            if(s[i][j]==ch)
                return 1;
        }
    }
    return 0;
}

ll solve(int n,char ch)
{

    if(n==1)
    {
        ll ret=1ll*(getcharmax(s[n],ch)+1)*sz(s[n+1]);
        if(ret>lim)
            return lim;
        return ret;
    }
    ll mx=0;
    pair<bool,char> p1=isonechar(s[n]);

        ll m1=getcharmax(s[n],ch);
        pair<int,char> p2=getprefix(s[n]);
        pair<int,char> p3=getsuffix(s[n]);
        bool p4=isfind(1,n-1,ch);
        ll m2=0,m3=0,m4=0;
        if(p2.second==p3.second&&p2.second==ch&&p4)
        {
            m2=p2.first+p3.first+1;
        }
        bool p5= isfind(1,n-1,ch);
        bool p6= isfind(1,n-1,ch);
        if(p5)
        {
            m3= p3.first+1;
        }
        if(p6)
        {
            m4=p2.first+1;
        }
        mx= max(m1,max(m2,max(m3,m4)));

    if(p1.first==1&&p1.second==ch)
    {

          mx=max(mx,(solve(n-1,ch)));
    }
    //cout<<mx<<endl;
     ll ret=1ll*(mx+1)*sz(s[n+1]);
     if(ret>lim)
        return lim;
     return ret;
}

int main()
{

    cin>>n;
    s.pb("");
    for(int i=1;i<=n;i++)
    {
        string str;
        cin>>str;
        s.pb(str);
    }

    pair<bool,char> p=isonechar(s[n]);

        int m1= getmax(s[n]);
        pair<int,char> p1=getprefix(s[n]);
        pair<int ,char> p2=getsuffix(s[n]);
        bool p3= isfind(1,n-1,p1.second);
        int m2=0,m3=0,m4=0;
        if(p1.second==p2.second&&p3)
        {
            m2=p1.first+p2.first+1;
        }
        bool p4= isfind(1,n-1,s[n][sz(s[n])-1]);
        bool p5= isfind(1,n-1,s[n][0]);
        if(p4)
        {
            m3= p2.first+1;
        }
        if(p5)
        {
            m4=p1.first+1;
        }

        int mx=max(m1,max(m2,max(m3,m4)));

        if(p.first==0)
        {

          cout<<mx<<endl;
          return 0;
        }

    int cnt=solve(n-1,p.second);
   //cout<<cnt<<endl;
    ll ans= max(mx,(cnt));
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define sz(v)   ((int)(v).size())
#define  all(v)    ((v).begin()),((v).end())
#define  allr(v)    ((v).rbegin()),((v).rend())
#define   pb         push_back
#define   mp         make_pair
#define   mt         make_tuple
//#define   Y            imag()
//#define   X            real()
#define    clr(v,d)      memset( v, d ,sizeof(v))
#define   angle(n)      atan2((n.imag()),(n.real()))
#define   vec(a,b)       ((b)-(a))
#define   length(a)      hypot( (a.imag()),(a.real()) )
#define   normalize(a)      (a)/(length(a))
//#define    dp(a,b)          (((conj(a))*(b)).real())
//#define    cp(a,b)          (((conj(a))*(b)).imag())
#define    lengthsqrt(a)       dp(a,a)
#define    rotate0( a,ang)    ((a)*exp( point(0,ang) ))
#define    rotateA(about,p,ang)   (rotate0(vec(about,p),ang)+about)
#define    lcm(a,b)                ((a*b)/(__gcd(a,b)))
#define    reflection0(m,v)         (conj((v)/(m))*(m))
#define     reflectionA(m,v,p0)     (conj( (vec(p0,v))/(vec(p0,m)) ) * (vec(p0,m)) ) + p0
//#define     same(p1,p2)               ( dp(  vec(p1,p2),vec(p1,p2)) < eps )
#define     PT                    complex<double>
#define outfile freopen("out.out", "w", stdout);
#define infile  freopen("in.in", "r", stdin);
#define PI acos(-1)
typedef  long long     ll ;
typedef  unsigned long long ull;
const double eps= (1e-9);
using namespace std;
int dcmp(long double a,long double b){   return fabsl(a-b)<=eps ? 0: (a>b)? 1:-1  ;}
int getBit(int num, int idx) {return ((num >> idx) & 1) == 1;}
int setBit1(int num, int idx) {return num | (1<<idx);}
ll setBit0(ll num, int idx) {return num & ~(1ll<<idx);}
ll flipBit(int num, int idx) {return num ^ (1<<idx);}
void FS(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);}
int countNumBit1(int mask) {int ret=0; while (mask)     {mask &= (mask-1);++ret;    }return ret;}
const int N=100009;
string s[N];
int n,vis[27];
int longest(string &s,char c)
{
    int mx=0,cnt=0;
    for(int i=0;i<sz(s);i++)
    {
        if(s[i]==c)
            cnt++;
        else
           cnt=0;
         mx=max(mx,cnt);
    }
    return mx;
}
int getP(string &s,char c)
{
    int cnt=0;
    for(int i=0;i<sz(s);i++)
    {
        if(s[i]==c)
            cnt++;
        else
            break;
    }
    return cnt;
}
int getS(string &s,char c)
{
    int cnt=0;
    for(int i=sz(s)-1;i>=0;i--)
    {
        if(s[i]==c)
            cnt++;
        else
            break;
    }
    return cnt;
}
int solve(int n,char c)
{
    int res= longest(s[1],c);
    for(int i=2;i<=n;i++)
    {
        int lgs=longest(s[i],c);
        int prefix=getP(s[i],c);
        int suffix=getS(s[i],c);

        if(lgs==sz(s[i]))
        {
            res= res+lgs*(res+1);
        }
        else if(res==0)
        {
            res=lgs;
        }
        else
        {
           res= max(prefix+suffix+1,lgs);
        }
    }
    return res;
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>s[i];
        for(int j=0;j<sz(s[i]);j++)
        {
            vis[s[i][j]-'a']++;
        }
    }
    int ans=1;
    for(int i=0;i<26;i++)
        if(vis[i]>0)
        ans=max(ans,solve(n,i+'a'));
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define sz(v)   ((int)(v).size())
#define  all(v)    ((v).begin()),((v).end())
#define  allr(v)    ((v).rbegin()),((v).rend())
#define   pb         push_back
#define   mp         make_pair
#define   mt         make_tuple
//#define   Y            imag()
//#define   X            real()
#define    clr(v,d)      memset( v, d ,sizeof(v))
#define   angle(n)      atan2((n.imag()),(n.real()))
#define   vec(a,b)       ((b)-(a))
#define   length(a)      hypot( (a.imag()),(a.real()) )
#define   normalize(a)      (a)/(length(a))
//#define    dp(a,b)          (((conj(a))*(b)).real())
//#define    cp(a,b)          (((conj(a))*(b)).imag())
#define    lengthsqrt(a)       dp(a,a)
#define    rotate0( a,ang)    ((a)*exp( point(0,ang) ))
#define    rotateA(about,p,ang)   (rotate0(vec(about,p),ang)+about)
#define    lcm(a,b)                ((a*b)/(__gcd(a,b)))
#define    reflection0(m,v)         (conj((v)/(m))*(m))
#define     reflectionA(m,v,p0)     (conj( (vec(p0,v))/(vec(p0,m)) ) * (vec(p0,m)) ) + p0
//#define     same(p1,p2)               ( dp(  vec(p1,p2),vec(p1,p2)) < eps )
#define     PT                    complex<double>
#define outfile freopen("out.out", "w", stdout);
#define infile  freopen("in.in", "r", stdin);
#define PI acos(-1)
typedef  long long     ll ;
typedef  unsigned long long ull;
const double eps= (1e-9);
using namespace std;
int dcmp(long double a,long double b){   return fabsl(a-b)<=eps ? 0: (a>b)? 1:-1  ;}
int getBit(int num, int idx) {return ((num >> idx) & 1) == 1;}
int setBit1(int num, int idx) {return num | (1<<idx);}
ll setBit0(ll num, int idx) {return num & ~(1ll<<idx);}
ll flipBit(int num, int idx) {return num ^ (1<<idx);}
void FS(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);}
int countNumBit1(int mask) {int ret=0; while (mask)     {mask &= (mask-1);++ret;    }return ret;}
int n;
vector< string > s;
int getmax(string st)
{
    int mx=0,cnt=0;
    for(int i=0;i<sz(st);i++)
    {
        if(i+1<sz(st)&&st[i]==st[i+1])
        {
            cnt++;
        }
        else
        {
            cnt++;
            mx=max(mx,cnt);
            cnt=0;
        }
    }
    mx=max(cnt,mx);
    return mx;
}
int getcharmax(string st,char ch)
{
    int mx=0,cnt=0;
    for(int i=0;i<sz(st)-1;i++)
    {
        if(st[i]==ch)
        {
            cnt++;
        }
        else
        {
            mx=max(mx,cnt);
            cnt=0;
        }
    }
    mx=max(cnt,mx);
    return mx;
}
pair<int,char> getprefix(string st)
{
    int cnt=0;
    for(int i=0;i<sz(st);i++)
    {
        if(i+1<sz(st)&&st[i]!=st[i+1])
        {
            cnt++;
            break;
        }
        else
        {
            cnt++;
        }
    }
    return mp(cnt,st[0]);
}
pair<bool,char> isonechar(string st)
{
    char ch=st[0];
    int n=sz(st);
    for(int i=0;i<n;i++)
    {
        if(st[i]!=ch)
            return mp(0,'a');
    }
    return mp(1,ch);
}
pair<int,char> getsuffix(string st)
{
    int cnt=0;
    int n=sz(st);
    for(int i=n-1;i>=0;i--)
    {
        if(i-1>=0&&st[i]!=st[i-1])
        {
            cnt++;
            break;
        }
        else
        {
            cnt++;
        }
    }
    return mp(cnt,st[n-1]);
}
bool isfind(int l,int r,char ch)
{
    for(int i=l;i<=r;i++)
    {
        for(int j=0;j<sz(s[i]);j++)
        {
            if(s[i][j]==ch)
                return 1;
        }
    }
    return 0;
}

int solve(int n,char ch)
{

    if(n==1)
    {
        return getcharmax(s[n],ch);
    }
    int mx=0;
    pair<bool,char> p1=isonechar(s[n]);

        ll m1=getcharmax(s[n],ch);
        pair<int,char> p2=getprefix(s[n]);
        pair<int,char> p3=getsuffix(s[n]);
        bool p4=isfind(1,n-1,ch);
        ll m2=0,m3=0;
        if(p2.second==p3.second&&p2.second==ch&&p4)
        {
            m2=p2.first+p3.first+1;
        }
       // cout<<m1<<" "<<m2<<" "<<m3<<endl;
        mx= max(m1,max(m2,m3));

    if(p1.first==1&&p1.second==ch)
    {
          mx=max(mx,(solve(n-1,ch)+1));
    }
    return mx*sz(s[n+1]);
}

int main()
{

    cin>>n;
    s.pb("");
    for(int i=1;i<=n;i++)
    {
        string str;
        cin>>str;
        s.pb(str);
    }

    pair<bool,char> p=isonechar(s[n]);

        int m1= getmax(s[n]);
        pair<int,char> p1=getprefix(s[n]);
        pair<int ,char> p2=getsuffix(s[n]);
        bool p3= isfind(1,n-1,p1.second);
        int m2=0;
        if(p1.second==p2.second&&p3)
        {
            m2=p1.first+p2.first+1;
        }

        if(p.first==0)
        {

          cout<<max(m1,m2)<<endl;
          return 0;
        }

    int cnt=solve(n-1,p.second);
  //  cout<<m1<<" "<<m2<<endl;
    ll ans= max(m1,max(m2,(cnt)));
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define sz(v)   ((int)(v).size())
#define  all(v)    ((v).begin()),((v).end())
#define  allr(v)    ((v).rbegin()),((v).rend())
#define   pb         push_back
#define   mp         make_pair
#define   mt         make_tuple
//#define   Y            imag()
//#define   X            real()
#define    clr(v,d)      memset( v, d ,sizeof(v))
#define   angle(n)      atan2((n.imag()),(n.real()))
#define   vec(a,b)       ((b)-(a))
#define   length(a)      hypot( (a.imag()),(a.real()) )
#define   normalize(a)      (a)/(length(a))
//#define    dp(a,b)          (((conj(a))*(b)).real())
//#define    cp(a,b)          (((conj(a))*(b)).imag())
#define    lengthsqrt(a)       dp(a,a)
#define    rotate0( a,ang)    ((a)*exp( point(0,ang) ))
#define    rotateA(about,p,ang)   (rotate0(vec(about,p),ang)+about)
#define    lcm(a,b)                ((a*b)/(__gcd(a,b)))
#define    reflection0(m,v)         (conj((v)/(m))*(m))
#define     reflectionA(m,v,p0)     (conj( (vec(p0,v))/(vec(p0,m)) ) * (vec(p0,m)) ) + p0
//#define     same(p1,p2)               ( dp(  vec(p1,p2),vec(p1,p2)) < eps )
#define     PT                    complex<double>
#define outfile freopen("out.out", "w", stdout);
#define infile  freopen("in.in", "r", stdin);
#define PI acos(-1)
typedef  long long     ll ;
typedef  unsigned long long ull;
const double eps= (1e-9);
using namespace std;
int dcmp(long double a,long double b){   return fabsl(a-b)<=eps ? 0: (a>b)? 1:-1  ;}
int getBit(int num, int idx) {return ((num >> idx) & 1) == 1;}
int setBit1(int num, int idx) {return num | (1<<idx);}
ll setBit0(ll num, int idx) {return num & ~(1ll<<idx);}
ll flipBit(int num, int idx) {return num ^ (1<<idx);}
void FS(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);}
int countNumBit1(int mask) {int ret=0; while (mask)     {mask &= (mask-1);++ret;    }return ret;}
int n;
vector< string > s;
int getmax(string st)
{
    int mx=0,cnt=0;
    for(int i=0;i<sz(st)-1;i++)
    {
        if(st[i]==st[i+1])
        {
            cnt++;
        }
        else
        {
            cnt++;
            mx=max(mx,cnt);
            cnt=0;
        }
    }
    mx=max(cnt,mx);
    return mx;
}
int getcharmax(string st,char ch)
{
    int mx=0,cnt=0;
    for(int i=0;i<sz(st)-1;i++)
    {
        if(st[i]==ch)
        {
            cnt++;
        }
        else
        {
            mx=max(mx,cnt);
            cnt=0;
        }
    }
    mx=max(cnt,mx);
    return mx;
}
pair<int,char> getprefix(string st)
{
    int cnt=0;
    for(int i=0;i<sz(st)-1;i++)
    {
        if(st[i]!=st[i+1])
        {
            cnt++;
            break;
        }
        else
        {
            cnt++;
        }
    }
    return mp(cnt,st[0]);
}
pair<bool,char> isonechar(string st)
{
    char ch=st[0];
    int n=sz(st);
    for(int i=0;i<n;i++)
    {
        if(st[i]!=ch)
            return mp(0,'a');
    }
    return mp(1,ch);
}
pair<int,char> getsuffix(string st)
{
    int cnt=0;
    int n=sz(st);
    for(int i=n-1;i>=0;i--)
    {
        if(st[i]!=st[i-1])
        {
            cnt++;
            break;
        }
        else
        {
            cnt++;
        }
    }
    return mp(cnt,st[n-1]);
}
bool isfind(int l,int r,char ch)
{
    for(int i=l;i<=r;i++)
    {
        for(int j=0;j<sz(s[i]);j++)
        {
            if(s[i][j]==ch)
                return 1;
        }
    }
    return 0;
}

ll solve(int n,char ch)
{

    if(n==1)
    {
        return getcharmax(s[n],ch);
    }

    pair<bool,char> p1=isonechar(s[n]);
    if((p1.first==0)||(p1.first==1&&p1.second!=ch))
    {
        ll m1=getcharmax(s[n],ch);
        pair<int,char> p2=getprefix(s[n]);
        pair<int,char> p3=getsuffix(s[n]);

        pair<int,char> p5=getprefix(s[n+1]);
        pair<int,char> p6=getsuffix(s[n+1]);

        bool p4=isfind(1,n-1,ch);
        bool p7=isfind(1,n,ch);
        ll m2=0,m3=0;
        if(p2.second==p3.second&&p2.second==ch&&p4)
        {
            m2=p2.first+p3.first+1;
        }
        if(p5.second==p6.second&&p5.second==ch&&p7)
        {
            m3=p5.first+p6.first+1;
        }
       // cout<<m1<<" "<<m2<<" "<<m3<<endl;
        return max(m1,max(m2,m3));
    }
    else if(p1.first==1&&p1.second==ch)
    {

          return sz(s[n])*(solve(n-1,ch)+1);
    }

    return 0;
}

int main()
{

    cin>>n;
    s.pb("");
    for(int i=1;i<=n;i++)
    {
        string str;
        cin>>str;
        s.pb(str);
    }

    pair<bool,char> p=isonechar(s[n]);
    if(p.first==0)
    {
        int m1= getmax(s[n]);
        pair<int,char> p1=getprefix(s[n]);
        pair<int ,char> p2=getsuffix(s[n]);
        bool p3= isfind(1,n-1,p1.second);
        int m2=0;
        if(p1.second==p2.second&&p3)
        {
            m2=p1.first+p2.first+1;
        }
        cout<<max(m1,m2)<<endl;
        return 0;
    }

    ll cnt=solve(n-1,p.second);

    ll ans= (cnt+1)*(sz(s[n]));
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define sz(v)   ((int)(v).size())
#define  all(v)    ((v).begin()),((v).end())
#define  allr(v)    ((v).rbegin()),((v).rend())
#define   pb         push_back
#define   mp         make_pair
#define   mt         make_tuple
//#define   Y            imag()
//#define   X            real()
#define    clr(v,d)      memset( v, d ,sizeof(v))
#define   angle(n)      atan2((n.imag()),(n.real()))
#define   vec(a,b)       ((b)-(a))
#define   length(a)      hypot( (a.imag()),(a.real()) )
#define   normalize(a)      (a)/(length(a))
//#define    dp(a,b)          (((conj(a))*(b)).real())
//#define    cp(a,b)          (((conj(a))*(b)).imag())
#define    lengthsqrt(a)       dp(a,a)
#define    rotate0( a,ang)    ((a)*exp( point(0,ang) ))
#define    rotateA(about,p,ang)   (rotate0(vec(about,p),ang)+about)
#define    lcm(a,b)                ((a*b)/(__gcd(a,b)))
#define    reflection0(m,v)         (conj((v)/(m))*(m))
#define     reflectionA(m,v,p0)     (conj( (vec(p0,v))/(vec(p0,m)) ) * (vec(p0,m)) ) + p0
//#define     same(p1,p2)               ( dp(  vec(p1,p2),vec(p1,p2)) < eps )
#define     PT                    complex<double>
#define outfile freopen("out.out", "w", stdout);
#define infile  freopen("in.in", "r", stdin);
#define PI acos(-1)
typedef  long long     ll ;
typedef  unsigned long long ull;
const double eps= (1e-9);
using namespace std;
int dcmp(long double a,long double b){   return fabsl(a-b)<=eps ? 0: (a>b)? 1:-1  ;}
int getBit(int num, int idx) {return ((num >> idx) & 1) == 1;}
int setBit1(int num, int idx) {return num | (1<<idx);}
ll setBit0(ll num, int idx) {return num & ~(1ll<<idx);}
ll flipBit(int num, int idx) {return num ^ (1<<idx);}
void FS(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);}
int countNumBit1(int mask) {int ret=0; while (mask)     {mask &= (mask-1);++ret;    }return ret;}
int n;
vector< string > s;
int getmax(string st)
{
    int mx=0,cnt=0;
    for(int i=0;i<sz(st);i++)
    {
        if(i+1<sz(st)&&st[i]==st[i+1])
        {
            cnt++;
        }
        else
        {
            cnt++;
            mx=max(mx,cnt);
            cnt=0;
        }
    }
    mx=max(cnt,mx);
    return mx;
}
int getcharmax(string st,char ch)
{
    int mx=0,cnt=0;
    for(int i=0;i<sz(st);i++)
    {
        if(st[i]==ch)
        {
            cnt++;
        }
        else
        {
            mx=max(mx,cnt);
            cnt=0;
        }
    }
    mx=max(cnt,mx);
    return mx;
}
pair<int,char> getprefix(string st)
{
    int cnt=0;
    for(int i=0;i<sz(st);i++)
    {
        if(i+1<sz(st)&&st[i]!=st[i+1])
        {
            cnt++;
            break;
        }
        else
        {
            cnt++;
        }
    }
    return mp(cnt,st[0]);
}
pair<bool,char> isonechar(string st)
{
    char ch=st[0];
    int n=sz(st);
    for(int i=0;i<n;i++)
    {
        if(st[i]!=ch)
            return mp(0,'a');
    }
    return mp(1,ch);
}
pair<int,char> getsuffix(string st)
{
    int cnt=0;
    int n=sz(st);
    for(int i=n-1;i>=0;i--)
    {
        if(i-1>=0&&st[i]!=st[i-1])
        {
            cnt++;
            break;
        }
        else
        {
            cnt++;
        }
    }
    return mp(cnt,st[n-1]);
}
bool isfind(int l,int r,char ch)
{
    for(int i=l;i<=r;i++)
    {
        for(int j=0;j<sz(s[i]);j++)
        {
            if(s[i][j]==ch)
                return 1;
        }
    }
    return 0;
}

int solve(int n,char ch)
{

    if(n==1)
    {
        return (getcharmax(s[n],ch)+1)*sz(s[n+1]);
    }
    int mx=0;
    pair<bool,char> p1=isonechar(s[n]);

        ll m1=getcharmax(s[n],ch);
        pair<int,char> p2=getprefix(s[n]);
        pair<int,char> p3=getsuffix(s[n]);
        bool p4=isfind(1,n-1,ch);
        ll m2=0,m3=0,m4=0;
        if(p2.second==p3.second&&p2.second==ch&&p4)
        {
            m2=p2.first+p3.first+1;
        }
        bool p5= isfind(1,n-1,s[n][sz(s[n])-1]);
        bool p6= isfind(1,n-1,s[n][0]);
        if(p5)
        {
            m3= p3.first+1;
        }
        if(p6)
        {
            m4=p2.first+1;
        }
        mx= max(m1,max(m2,max(m3,m4)));

    if(p1.first==1&&p1.second==ch)
    {
          mx=max(mx,(solve(n-1,ch)));
    }
    return (mx+1)*sz(s[n+1]);
}

int main()
{

    cin>>n;
    s.pb("");
    for(int i=1;i<=n;i++)
    {
        string str;
        cin>>str;
        s.pb(str);
    }

    pair<bool,char> p=isonechar(s[n]);

        int m1= getmax(s[n]);
        pair<int,char> p1=getprefix(s[n]);
        pair<int ,char> p2=getsuffix(s[n]);
        bool p3= isfind(1,n-1,p1.second);
        int m2=0,m3=0,m4=0;
        if(p1.second==p2.second&&p3)
        {
            m2=p1.first+p2.first+1;
        }
        bool p4= isfind(1,n-1,s[n][sz(s[n])-1]);
        bool p5= isfind(1,n-1,s[n][0]);
        if(p4)
        {
            m3= p2.first+1;
        }
        if(p5)
        {
            m4=p1.first+1;
        }

        int mx=max(m1,max(m2,max(m3,m4)));

        if(p.first==0)
        {

          cout<<mx<<endl;
          return 0;
        }

    int cnt=solve(n-1,p.second);
  //  cout<<m1<<" "<<m2<<endl;
    ll ans= max(mx,(cnt));
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define sz(v)   ((int)(v).size())
#define  all(v)    ((v).begin()),((v).end())
#define  allr(v)    ((v).rbegin()),((v).rend())
#define   pb         push_back
#define   mp         make_pair
#define   mt         make_tuple
//#define   Y            imag()
//#define   X            real()
#define    clr(v,d)      memset( v, d ,sizeof(v))
#define   angle(n)      atan2((n.imag()),(n.real()))
#define   vec(a,b)       ((b)-(a))
#define   length(a)      hypot( (a.imag()),(a.real()) )
#define   normalize(a)      (a)/(length(a))
//#define    dp(a,b)          (((conj(a))*(b)).real())
//#define    cp(a,b)          (((conj(a))*(b)).imag())
#define    lengthsqrt(a)       dp(a,a)
#define    rotate0( a,ang)    ((a)*exp( point(0,ang) ))
#define    rotateA(about,p,ang)   (rotate0(vec(about,p),ang)+about)
#define    lcm(a,b)                ((a*b)/(__gcd(a,b)))
#define    reflection0(m,v)         (conj((v)/(m))*(m))
#define     reflectionA(m,v,p0)     (conj( (vec(p0,v))/(vec(p0,m)) ) * (vec(p0,m)) ) + p0
//#define     same(p1,p2)               ( dp(  vec(p1,p2),vec(p1,p2)) < eps )
#define     PT                    complex<double>
#define outfile freopen("out.out", "w", stdout);
#define infile  freopen("in.in", "r", stdin);
#define PI acos(-1)
typedef  long long     ll ;
typedef  unsigned long long ull;
const double eps= (1e-9);
using namespace std;
int dcmp(long double a,long double b){   return fabsl(a-b)<=eps ? 0: (a>b)? 1:-1  ;}
int getBit(int num, int idx) {return ((num >> idx) & 1) == 1;}
int setBit1(int num, int idx) {return num | (1<<idx);}
ll setBit0(ll num, int idx) {return num & ~(1ll<<idx);}
ll flipBit(int num, int idx) {return num ^ (1<<idx);}
void FS(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);}
int countNumBit1(int mask) {int ret=0; while (mask)     {mask &= (mask-1);++ret;    }return ret;}
int n;
vector< string > s;
int getmax(string st)
{
    int mx=0,cnt=0;
    for(int i=0;i<sz(st);i++)
    {
        if(i+1<sz(st)&&st[i]==st[i+1])
        {
            cnt++;
        }
        else
        {
            cnt++;
            mx=max(mx,cnt);
            cnt=0;
        }
    }
    mx=max(cnt,mx);
    return mx;
}
int getcharmax(string st,char ch)
{
    int mx=0,cnt=0;
    for(int i=0;i<sz(st)-1;i++)
    {
        if(st[i]==ch)
        {
            cnt++;
        }
        else
        {
            mx=max(mx,cnt);
            cnt=0;
        }
    }
    mx=max(cnt,mx);
    return mx;
}
pair<int,char> getprefix(string st)
{
    int cnt=0;
    for(int i=0;i<sz(st);i++)
    {
        if(i+1<sz(st)&&st[i]!=st[i+1])
        {
            cnt++;
            break;
        }
        else
        {
            cnt++;
        }
    }
    return mp(cnt,st[0]);
}
pair<bool,char> isonechar(string st)
{
    char ch=st[0];
    int n=sz(st);
    for(int i=0;i<n;i++)
    {
        if(st[i]!=ch)
            return mp(0,'a');
    }
    return mp(1,ch);
}
pair<int,char> getsuffix(string st)
{
    int cnt=0;
    int n=sz(st);
    for(int i=n-1;i>=0;i--)
    {
        if(i-1>=0&&st[i]!=st[i-1])
        {
            cnt++;
            break;
        }
        else
        {
            cnt++;
        }
    }
    return mp(cnt,st[n-1]);
}
bool isfind(int l,int r,char ch)
{
    for(int i=l;i<=r;i++)
    {
        for(int j=0;j<sz(s[i]);j++)
        {
            if(s[i][j]==ch)
                return 1;
        }
    }
    return 0;
}

int solve(int n,char ch)
{

    if(n==1)
    {
        return getcharmax(s[n],ch);
    }
    int mx=0;
    pair<bool,char> p1=isonechar(s[n]);

        ll m1=getcharmax(s[n],ch);
        pair<int,char> p2=getprefix(s[n]);
        pair<int,char> p3=getsuffix(s[n]);
        bool p4=isfind(1,n-1,ch);
        ll m2=0,m3=0,m4=0;
        if(p2.second==p3.second&&p2.second==ch&&p4)
        {
            m2=p2.first+p3.first+1;
        }
        bool p5= isfind(1,n-1,s[n][sz(s[n])-1]);
        bool p6= isfind(1,n-1,s[n][0]);
        if(p5)
        {
            m3= p3.first+1;
        }
        if(p6)
        {
            m4=p2.first+1;
        }
        mx= max(m1,max(m2,max(m3,m4)));

    if(p1.first==1&&p1.second==ch)
    {
          mx=max(mx,(solve(n-1,ch)+1));
    }
    return mx*sz(s[n+1]);
}

int main()
{

    cin>>n;
    s.pb("");
    for(int i=1;i<=n;i++)
    {
        string str;
        cin>>str;
        s.pb(str);
    }

    pair<bool,char> p=isonechar(s[n]);

        int m1= getmax(s[n]);
        pair<int,char> p1=getprefix(s[n]);
        pair<int ,char> p2=getsuffix(s[n]);
        bool p3= isfind(1,n-1,p1.second);
        int m2=0,m3=0,m4=0;
        if(p1.second==p2.second&&p3)
        {
            m2=p1.first+p2.first+1;
        }
        bool p4= isfind(1,n-1,s[n][sz(s[n])-1]);
        bool p5= isfind(1,n-1,s[n][0]);
        if(p4)
        {
            m3= p2.first+1;
        }
        if(p5)
        {
            m4=p1.first+1;
        }

        int mx=max(m1,max(m2,max(m3,m4)));

        if(p.first==0)
        {

          cout<<mx<<endl;
          return 0;
        }

    int cnt=solve(n-1,p.second);
  //  cout<<m1<<" "<<m2<<endl;
    ll ans= max(mx,(cnt));
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
#define sz(v)   ((int)(v).size())
#define  all(v)    ((v).begin()),((v).end())
#define  allr(v)    ((v).rbegin()),((v).rend())
#define   pb         push_back
#define   mp         make_pair
#define   mt         make_tuple
//#define   Y            imag()
//#define   X            real()
#define    clr(v,d)      memset( v, d ,sizeof(v))
#define   angle(n)      atan2((n.imag()),(n.real()))
#define   vec(a,b)       ((b)-(a))
#define   length(a)      hypot( (a.imag()),(a.real()) )
#define   normalize(a)      (a)/(length(a))
//#define    dp(a,b)          (((conj(a))*(b)).real())
//#define    cp(a,b)          (((conj(a))*(b)).imag())
#define    lengthsqrt(a)       dp(a,a)
#define    rotate0( a,ang)    ((a)*exp( point(0,ang) ))
#define    rotateA(about,p,ang)   (rotate0(vec(about,p),ang)+about)
#define    lcm(a,b)                ((a*b)/(__gcd(a,b)))
#define    reflection0(m,v)         (conj((v)/(m))*(m))
#define     reflectionA(m,v,p0)     (conj( (vec(p0,v))/(vec(p0,m)) ) * (vec(p0,m)) ) + p0
//#define     same(p1,p2)               ( dp(  vec(p1,p2),vec(p1,p2)) < eps )
#define     PT                    complex<double>
#define outfile freopen("out.out", "w", stdout);
#define infile  freopen("in.in", "r", stdin);
#define PI acos(-1)
typedef  long long     ll ;
typedef  unsigned long long ull;
const double eps= (1e-9);
using namespace std;
int dcmp(long double a,long double b){   return fabsl(a-b)<=eps ? 0: (a>b)? 1:-1  ;}
int getBit(int num, int idx) {return ((num >> idx) & 1) == 1;}
int setBit1(int num, int idx) {return num | (1<<idx);}
ll setBit0(ll num, int idx) {return num & ~(1ll<<idx);}
ll flipBit(int num, int idx) {return num ^ (1<<idx);}
void FS(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);}
int countNumBit1(int mask) {int ret=0; while (mask)     {mask &= (mask-1);++ret;    }return ret;}
int n;
vector< string > s;
int getmax(string st)
{
    int mx=0,cnt=0;
    for(int i=0;i<sz(st);i++)
    {
        if(i+1<sz(st)&&st[i]==st[i+1])
        {
            cnt++;
        }
        else
        {
            cnt++;
            mx=max(mx,cnt);
            cnt=0;
        }
    }
    mx=max(cnt,mx);
    return mx;
}
int getcharmax(string st,char ch)
{
    int mx=0,cnt=0;
    for(int i=0;i<sz(st);i++)
    {
        if(st[i]==ch)
        {
            cnt++;
        }
        else
        {
            mx=max(mx,cnt);
            cnt=0;
        }
    }
    mx=max(cnt,mx);
    return mx;
}
pair<int,char> getprefix(string st)
{
    int cnt=0;
    for(int i=0;i<sz(st);i++)
    {
        if(i+1<sz(st)&&st[i]!=st[i+1])
        {
            cnt++;
            break;
        }
        else
        {
            cnt++;
        }
    }
    return mp(cnt,st[0]);
}
pair<bool,char> isonechar(string st)
{
    char ch=st[0];
    int n=sz(st);
    for(int i=0;i<n;i++)
    {
        if(st[i]!=ch)
            return mp(0,'a');
    }
    return mp(1,ch);
}
pair<int,char> getsuffix(string st)
{
    int cnt=0;
    int n=sz(st);
    for(int i=n-1;i>=0;i--)
    {
        if(i-1>=0&&st[i]!=st[i-1])
        {
            cnt++;
            break;
        }
        else
        {
            cnt++;
        }
    }
    return mp(cnt,st[n-1]);
}
bool isfind(int l,int r,char ch)
{
    for(int i=l;i<=r;i++)
    {
        for(int j=0;j<sz(s[i]);j++)
        {
            if(s[i][j]==ch)
                return 1;
        }
    }
    return 0;
}

int solve(int n,char ch)
{

    if(n==1)
    {
        return (getcharmax(s[n],ch)+1)*sz(s[n+1]);
    }
    int mx=0;
    pair<bool,char> p1=isonechar(s[n]);

        ll m1=getcharmax(s[n],ch);
        pair<int,char> p2=getprefix(s[n]);
        pair<int,char> p3=getsuffix(s[n]);
        bool p4=isfind(1,n-1,ch);
        ll m2=0,m3=0,m4=0;
        if(p2.second==p3.second&&p2.second==ch&&p4)
        {
            m2=p2.first+p3.first+1;
        }
        bool p5= isfind(1,n-1,ch);
        bool p6= isfind(1,n-1,ch);
        if(p5)
        {
            m3= p3.first+1;
        }
        if(p6)
        {
            m4=p2.first+1;
        }
        mx= max(m1,max(m2,max(m3,m4)));

    if(p1.first==1&&p1.second==ch)
    {

          mx=max(mx,(solve(n-1,ch)));
    }
    //cout<<mx<<endl;
    return (mx+1)*sz(s[n+1]);
}

int main()
{

    cin>>n;
    s.pb("");
    for(int i=1;i<=n;i++)
    {
        string str;
        cin>>str;
        s.pb(str);
    }

    pair<bool,char> p=isonechar(s[n]);

        int m1= getmax(s[n]);
        pair<int,char> p1=getprefix(s[n]);
        pair<int ,char> p2=getsuffix(s[n]);
        bool p3= isfind(1,n-1,p1.second);
        int m2=0,m3=0,m4=0;
        if(p1.second==p2.second&&p3)
        {
            m2=p1.first+p2.first+1;
        }
        bool p4= isfind(1,n-1,s[n][sz(s[n])-1]);
        bool p5= isfind(1,n-1,s[n][0]);
        if(p4)
        {
            m3= p2.first+1;
        }
        if(p5)
        {
            m4=p1.first+1;
        }

        int mx=max(m1,max(m2,max(m3,m4)));

        if(p.first==0)
        {

          cout<<mx<<endl;
          return 0;
        }

    int cnt=solve(n-1,p.second);
   //cout<<cnt<<endl;
    ll ans= max(mx,(cnt));
    cout<<ans<<endl;
}