#include <algorithm>
#include <stack>
#include <istream>
#include <stdio.h>
#include <map>
#include <math.h>
#include <vector>
#include <iostream>
#include <queue>
#include <string.h>
#include <set>
#include <cstdio>
#define FR(i,n) for(int i=0;i<n;i++)
#define MAX 2005
#define mkp pair <int,int>
using namespace std;
const int maxn = 2e5 + 1000;
typedef long long ll;
const int  inf = 1e6;
void read(int  &x) {
	char ch; bool flag = 0;
	for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
	for (x = 0; isdigit(ch); x = (x << 1) + (x << 3) + ch - 48, ch = getchar());
	x *= 1 - 2 * flag;
}
int n,m;

vector <int> vec[maxn];
int dep[maxn],fa[maxn];

int dfs(int cnt,int la)
{
    int Max = 0;
    int siz = vec[cnt].size();

    for(int i=0;i<siz;i++)
    {
        int now  = vec[cnt][i];
        if(now==la)continue;
        dep[now] = dep[cnt]+1;
        Max=max(dfs(now,cnt),Max);
    }
    fa[cnt]=Max;
    return Max+1;
}
bool flag = true;
int vis[maxn];
int path[maxn],tot = 0;
void pdfs(int cnt,int fa)
{
    int siz = vec[cnt].size();
    if(cnt==m)
    {
        ///printf("((");
        flag = false;
        return ;
    }
   /// cout<<siz<<endl;
   /// printf("***\n");
    for(int i=0;i<siz;i++)
    {
        path[tot++]=vec[cnt][i];
        int now = vec[cnt][i];
       // cout<<fa<<endl;
        if(fa==now)continue;
        ///printf("&***");
        pdfs(now,cnt);
        if(!flag)return ;
    }
}
int main() {
    memset(dep,0,sizeof(dep));
    memset(fa,0,sizeof(fa));
    read(n),read(m);
    for(int i=0;i<n-1;i++){
        int a,b;read(a),read(b);
        vec[a].push_back(b),vec[b].push_back(a);
    }
    dfs(1,-1);
    pdfs(1,-1);
    int ans = 0;
   /// cout<<tot<<endl;
    ///for(int i=1;i<=n;i++)
    memset(vis,0,sizeof(vis));
    for(int i=tot-1,j=1;i>=0;i--,j++)
    {
        vis[j]=1;
     ///   printf("%d\n",path[i]);
        if(!vis[i])ans=max(ans,dep[path[i]]+fa[path[i]]);
    }
    cout<<ans*2<<endl;
    return 0;
}


#include <algorithm>
#include <stack>
#include <istream>
#include <stdio.h>
#include <map>
#include <math.h>
#include <vector>
#include <iostream>
#include <queue>
#include <string.h>
#include <set>
#include <cstdio>
#define FR(i,n) for(int i=0;i<n;i++)
#define MAX 2005
#define mkp pair <int,int>
using namespace std;
const int maxn = 2e5 + 1000;
typedef long long ll;
const int  inf = 1e6;
void read(int  &x) {
	char ch; bool flag = 0;
	for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
	for (x = 0; isdigit(ch); x = (x << 1) + (x << 3) + ch - 48, ch = getchar());
	x *= 1 - 2 * flag;
}
int n,m;

vector <int> vec[maxn];
int dep[maxn],fa[maxn];

int dfs(int cnt,int la)
{
    int Max = 0;
    int siz = vec[cnt].size();

    for(int i=0;i<siz;i++)
    {
        int now  = vec[cnt][i];
        if(now==la)continue;
        dep[now] = dep[cnt]+1;
        Max=max(dfs(now,cnt),Max);
    }
    ///fa[cnt]=Max+1;
    return fa[cnt]=Max+1;
}
bool flag = true;
int vis[maxn];
int path[maxn],tot = 2;
void pdfs(int cnt,int fa,int now)
{
    if(!flag)return ;
    int siz = vec[cnt].size();
   /// printf("%d**\n",cnt);
    //path[now]=cnt;
    if(cnt==m)
    {
        //tot = now;
        flag = false;
        return ;
    }
    for(int i=0;i<siz;i++)
    {

        int no = vec[cnt][i];
        if(fa==no)continue;
        //tot++;
        path[tot]=no;
        tot++;
        ///tot++;
        pdfs(no,cnt,now+1);
        if(!flag)return ;
    }
}
int main() {
    memset(dep,0,sizeof(dep));
    memset(fa,0,sizeof(fa));
    read(n),read(m);
    for(int i=0;i<n-1;i++){
        int a,b;read(a),read(b);
        vec[a].push_back(b),vec[b].push_back(a);
    }
    dfs(1,-1);
    pdfs(1,-1,1);
    int ans = 0;
    path[1]=1;
    memset(vis,0,sizeof(vis));
   /// cout<<tot<<endl;
    for(int i=tot,j=1;i;i--,j++)
    {
        if(!vis[i])ans=max(ans,dep[path[i]]+fa[path[i]]-1);
    }
    cout<<ans*2<<endl;
    return 0;
}


#include <algorithm>
#include <stack>
#include <istream>
#include <stdio.h>
#include <map>
#include <math.h>
#include <vector>
#include <iostream>
#include <queue>
#include <string.h>
#include <set>
#include <cstdio>
#define FR(i,n) for(int i=0;i<n;i++)
#define MAX 2005
#define mkp pair <int,int>
using namespace std;
const int maxn = 2e5 + 1000;
typedef long long ll;
const int  inf = 1e6;
void read(int  &x) {
	char ch; bool flag = 0;
	for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
	for (x = 0; isdigit(ch); x = (x << 1) + (x << 3) + ch - 48, ch = getchar());
	x *= 1 - 2 * flag;
}
int n,m;

vector <int> vec[maxn];
int dep[maxn],fa[maxn];

int dfs(int cnt,int la)
{
    int Max = 0;
    int siz = vec[cnt].size();

    for(int i=0;i<siz;i++)
    {
        int now  = vec[cnt][i];
        if(now==la)continue;
        dep[now] = dep[cnt]+1;
        Max=max(dfs(now,cnt),Max);
    }
    ///fa[cnt]=Max+1;
    return fa[cnt]=Max+1;
}
bool flag = true;
int vis[maxn];
int path[maxn],tot = 1;
void pdfs(int cnt,int fa,int now)
{
    if(!flag)return ;
    int siz = vec[cnt].size();
    //path[now]=cnt;
    if(cnt==m)
    {
        //tot = now;
        flag = false;
        return ;
    }
    for(int i=0;i<siz;i++)
    {

        int no = vec[cnt][i];
        if(fa==no)continue;path[tot]=no;
        tot++;
        pdfs(no,cnt,now+1);
        if(!flag)return ;
    }
}
int main() {
    memset(dep,0,sizeof(dep));
    memset(fa,0,sizeof(fa));
    read(n),read(m);
    for(int i=0;i<n-1;i++){
        int a,b;read(a),read(b);
        vec[a].push_back(b),vec[b].push_back(a);
    }
    dfs(1,-1);
    pdfs(1,-1,1);
    int ans = 0;
    memset(vis,0,sizeof(vis));
    ///cout<<tot<<endl;
    for(int i=tot-1,j=1;i;i--,j++)
    {
        if(!vis[i])ans=max(ans,dep[path[i]]+fa[path[i]]-1);
    }
    cout<<ans*2<<endl;
    return 0;
}


#include <algorithm>
#include <stack>
#include <istream>
#include <stdio.h>
#include <map>
#include <math.h>
#include <vector>
#include <iostream>
#include <queue>
#include <string.h>
#include <set>
#include <cstdio>
#define FR(i,n) for(int i=0;i<n;i++)
#define MAX 2005
#define mkp pair <int,int>
using namespace std;
const int maxn = 2e5 + 1000;
typedef long long ll;
const int  inf = 1e6;
void read(int  &x) {
	char ch; bool flag = 0;
	for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
	for (x = 0; isdigit(ch); x = (x << 1) + (x << 3) + ch - 48, ch = getchar());
	x *= 1 - 2 * flag;
}
int n,m;

vector <int> vec[maxn];
int dep[maxn],fa[maxn];

int dfs(int cnt,int la)
{
    int Max = 0;
    int siz = vec[cnt].size();

    for(int i=0;i<siz;i++)
    {
        int now  = vec[cnt][i];
        if(now==la)continue;
        dep[now] = dep[cnt]+1;
        Max=max(dfs(now,cnt),Max);
    }
    ///fa[cnt]=Max+1;
    return fa[cnt]=Max+1;
}
bool flag = true;
int vis[maxn];
int path[maxn],tot = 1;
void pdfs(int cnt,int fa,int now)
{
    if(!flag)return ;
    int siz = vec[cnt].size();
   /// printf("%d**\n",cnt);
    //path[now]=cnt;
    if(cnt==m)
    {
        //tot = now;
        flag = false;
        return ;
    }
    for(int i=0;i<siz;i++)
    {

        int no = vec[cnt][i];
        if(fa==no)continue;path[++tot]=no;
        ///tot++;
        pdfs(no,cnt,now+1);
        if(!flag)return ;
    }
}
int main() {
    memset(dep,0,sizeof(dep));
    memset(fa,0,sizeof(fa));
    read(n),read(m);
    for(int i=0;i<n-1;i++){
        int a,b;read(a),read(b);
        vec[a].push_back(b),vec[b].push_back(a);
    }
    dfs(1,-1);
    pdfs(1,-1,1);
    int ans = 0;
    path[1]=1;
    memset(vis,0,sizeof(vis));
   /// cout<<tot<<endl;
    for(int i=tot,j=1;i;i--,j++)
    {
        if(!vis[i])ans=max(ans,dep[path[i]]+fa[path[i]]-1);
    }
    cout<<ans*2<<endl;
    return 0;
}


#include <algorithm>
#include <stack>
#include <istream>
#include <stdio.h>
#include <map>
#include <math.h>
#include <vector>
#include <iostream>
#include <queue>
#include <string.h>
#include <set>
#include <cstdio>
#define FR(i,n) for(int i=0;i<n;i++)
#define MAX 2005
#define mkp pair <int,int>
using namespace std;
const int maxn = 2e5 + 1000;
typedef long long ll;
const int  inf = 1e6;
void read(int  &x) {
	char ch; bool flag = 0;
	for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
	for (x = 0; isdigit(ch); x = (x << 1) + (x << 3) + ch - 48, ch = getchar());
	x *= 1 - 2 * flag;
}
int n,m;

vector <int> vec[maxn];
int dep[maxn],fa[maxn];

int dfs(int cnt,int la)
{
    int Max = 0;
    int siz = vec[cnt].size();

    for(int i=0;i<siz;i++)
    {
        int now  = vec[cnt][i];
        if(now==la)continue;
        dep[now] = dep[cnt]+1;
        Max=max(dfs(now,cnt),Max);
    }
    fa[cnt]=Max;
    return Max+1;
}
bool flag = true;
int vis[maxn];
int path[maxn],tot = 1;
void pdfs(int cnt,int fa)
{
    int siz = vec[cnt].size();
    if(cnt==m)
    {
        ///printf("((");
        flag = false;
        return ;
    }
    for(int i=0;i<siz;i++)
    {
        path[tot++]=vec[cnt][i];
        int now = vec[cnt][i];
       // cout<<fa<<endl;
        if(fa==now)continue;
        ///printf("&***");
        pdfs(now,cnt);
        if(!flag)return ;
    }
}
int main() {
    memset(dep,0,sizeof(dep));
    memset(fa,0,sizeof(fa));
    read(n),read(m);
    for(int i=0;i<n-1;i++){
        int a,b;read(a),read(b);
        vec[a].push_back(b),vec[b].push_back(a);
    }
    dfs(1,-1);
    pdfs(1,-1);
    int ans = 0;
    ///cout<<tot<<endl;
    memset(vis,0,sizeof(vis));
    for(int i=tot-1,j=1;i>=1;i--,j++)
    {
        vis[j]=1;
       /// printf("**))");
        if(!vis[path[i]])ans=max(ans,dep[path[i]]+fa[path[i]]);
    }
    cout<<ans*2<<endl;
    return 0;
}


#include <algorithm>
#include <stack>
#include <istream>
#include <stdio.h>
#include <map>
#include <math.h>
#include <vector>
#include <iostream>
#include <queue>
#include <string.h>
#include <set>
#include <cstdio>
#define FR(i,n) for(int i=0;i<n;i++)
#define MAX 2005
#define mkp pair <int,int>
using namespace std;
const int maxn = 2e5 + 1000;
typedef long long ll;
const int  inf = 1e6;
void read(int  &x) {
	char ch; bool flag = 0;
	for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
	for (x = 0; isdigit(ch); x = (x << 1) + (x << 3) + ch - 48, ch = getchar());
	x *= 1 - 2 * flag;
}
int n,m;

vector <int> vec[maxn];
int dep[maxn],fa[maxn];

int dfs(int cnt,int la)
{
    int Max = 0;
    int siz = vec[cnt].size();

    for(int i=0;i<siz;i++)
    {
        int now  = vec[cnt][i];
        if(now==la)continue;
        dep[now] = dep[cnt]+1;
        Max=max(dfs(now,cnt),Max);
    }
    ///fa[cnt]=Max+1;
    return fa[cnt]=Max+1;
}
bool flag = true;
int vis[maxn];
int path[maxn],tot = 1;
void pdfs(int cnt,int fa,int now )
{
    if(!flag)return ;
    int siz = vec[cnt].size();
    path[now]=cnt;
    //tot++;
    if(cnt==m)
    {
        tot = now;
        flag = false;
        return ;
    }
    for(int i=0;i<siz;i++)
    {
        int now = vec[cnt][i];
        if(fa==now)continue;
        pdfs(now,cnt,now+1);
        if(!flag)return ;
    }
}
int main() {
    memset(dep,0,sizeof(dep));
    memset(fa,0,sizeof(fa));
    read(n),read(m);
    for(int i=0;i<n-1;i++){
        int a,b;read(a),read(b);
        vec[a].push_back(b),vec[b].push_back(a);
    }
    dfs(1,-1);
    pdfs(1,-1,1);
    int ans = 0;
    memset(vis,0,sizeof(vis));
    ///cout<<tot<<endl;
    for(int i=tot,j=1;i;i--,j++)
    {
        vis[j]=1;
        if(!vis[i])ans=max(ans,dep[path[i]]+fa[path[i]]-1);
    }
    cout<<ans*2<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define postab g.tab[pos]

#define MAXN 200020
#define MAXM 200020

typedef vector<int> node;
typedef long long ll;

struct graph
{
    node tab[200020];

    graph(void){}/*
    graph(int n)
    {
        tab.resize(n+1);
    }

    void resize(int n)
    {
        tab.resize(n+1);
    }*/
    void addedge(int l,int r)
    {
        tab[l].push_back(r);
    }
};

struct point
{
    int l,r;
    ll dat,lazy;
};

int dep[MAXN],len[MAXN],fa[MAXN];
graph g;

int dfs(int pos,int td,int tf)
{
    int md=1;

    dep[pos]=td;
    fa[pos]=tf;
//cout<<"adsf";
    for(int i=0;i<g.tab[pos].size();i++)
    {
    	if(fa[g.tab[pos][i]]==0)
    	{
    		md=max(md,1+dfs(g.tab[pos][i],td+1,pos));
    	}

    }

    len[pos]=md;

    return md;
}

int main(void)
{
	ios::sync_with_stdio(false);
	int n,x,h,pos,j=0;

	cin>>n>>x;
	//g.resize(n+1);
	for(int i=1;i<n;i++)
	{
		int l,r;

		cin>>l>>r;
		g.addedge(l,r);
	}

	dfs(1,1,1);
	h=(dep[x]+1)/2+1;

	pos=x;
	while(dep[pos]!=h)
	{
		pos=fa[pos];
		j++;
	}
//cout<<dep[pos]<<" "<<len[pos];
	cout<<(len[pos]+dep[pos]-2)*2;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define postab g.tab[pos]

#define MAXN 200020
#define MAXM 200020

typedef vector<int> node;
typedef long long ll;

struct graph
{
    node tab[200020];

    graph(void){}/*
    graph(int n)
    {
        tab.resize(n+1);
    }

    void resize(int n)
    {
        tab.resize(n+1);
    }*/
    void addedge(int l,int r)
    {
        tab[l].push_back(r);
    }
};

struct point
{
    int l,r;
    ll dat,lazy;
};

int dep[MAXN],len[MAXN],fa[MAXN];
graph g;

int dfs(int pos,int td,int tf)
{
    int md=1;

    dep[pos]=td;
    fa[pos]=tf;
//cout<<"adsf";
    for(int i=0;i<g.tab[pos].size();i++)
    {
    	md=max(md,1+dfs(g.tab[pos][i],td+1,pos));
    }

    len[pos]=md;

    return md;
}

int main(void)
{
	ios::sync_with_stdio(false);
	int n,x,h,pos,j=0;

	cin>>n>>x;
	//g.resize(n+1);
	for(int i=1;i<n;i++)
	{
		int l,r;

		cin>>l>>r;
		g.addedge(l,r);
	}

	dfs(1,1,1);
	h=(dep[x]+1)/2+1;

	pos=x;
	while(dep[pos]!=h)
	{
		pos=fa[pos];
		j++;
	}
//cout<<dep[pos]<<" "<<len[pos];
	cout<<(len[pos]+dep[pos]-2)*2;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define postab g.tab[pos]

#define MAXN 200020
#define MAXM 200020

typedef vector<int> node;
typedef long long ll;

struct graph
{
    node tab[200020];

    graph(void){}/*
    graph(int n)
    {
        tab.resize(n+1);
    }

    void resize(int n)
    {
        tab.resize(n+1);
    }*/
    void addedge(int l,int r)
    {
        tab[l].push_back(r);
        tab[r].push_back(l);
    }
};

struct point
{
    int l,r;
    ll dat,lazy;
};

int dep[MAXN],len[MAXN],fa[MAXN];
graph g;

int dfs(int pos,int td,int tf)
{
    int md=1;

    dep[pos]=td;
    fa[pos]=tf;
//cout<<"adsf";
    for(int i=0;i<g.tab[pos].size();i++)
    {
    	if(fa[g.tab[pos][i]]==0)
    	{
    		md=max(md,1+dfs(g.tab[pos][i],td+1,pos));
    	}
    }

    len[pos]=md;

    return md;
}

int main(void)
{
	ios::sync_with_stdio(false);
	int n,x,h,pos,j=0;

	cin>>n>>x;
	//g.resize(n+1);
	for(int i=1;i<n;i++)
	{
		int l,r;

		cin>>l>>r;
		g.addedge(l,r);
	}

	dfs(1,1,1);
	h=(dep[x]+1)/2+1;

	pos=x;
	if(dep[x]==1)
	{
		cout<<"0";
		return 0;
	}
	while(dep[pos]!=h)
	{
		pos=fa[pos];
		j++;
	}
//cout<<dep[pos]<<" "<<len[pos];
	cout<<(len[pos]+dep[pos]-2)*2;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define postab g.tab[pos]

#define MAXN 200020
#define MAXM 200020

typedef vector<int> node;
typedef long long ll;

struct graph
{
    node tab[200020];

    graph(void){}/*
    graph(int n)
    {
        tab.resize(n+1);
    }

    void resize(int n)
    {
        tab.resize(n+1);
    }*/
    void addedge(int l,int r)
    {
        tab[l].push_back(r);
    }
};

struct point
{
    int l,r;
    ll dat,lazy;
};

int dep[MAXN],len[MAXN],fa[MAXN];
graph g;

int dfs(int pos,int td,int tf)
{
    int md=1;

    dep[pos]=td;
    fa[pos]=tf;
//cout<<"adsf";
    for(int i=0;i<g.tab[pos].size();i++)
    {
    	if(fa[g.tab[pos][i]]==0)
    	{
    		md=max(md,1+dfs(g.tab[pos][i],td+1,pos));
    	}

    }

    len[pos]=md;

    return md;
}

int main(void)
{
	ios::sync_with_stdio(false);
	int n,x,h,pos,j=0;

	cin>>n>>x;
	//g.resize(n+1);
	for(int i=1;i<n;i++)
	{
		int l,r;

		cin>>l>>r;
		g.addedge(l,r);
	}

	dfs(1,1,1);
	h=(dep[x]+1)/2+1;

	pos=x;
	while(dep[pos]!=h && dep[pos]>1)
	{
		pos=fa[pos];
		j++;
	}
//cout<<dep[pos]<<" "<<len[pos];
	cout<<(len[pos]+dep[pos]-2)*2;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define postab g.tab[pos]

#define MAXN 200020
#define MAXM 200020

typedef vector<int> node;
typedef long long ll;

struct graph
{
    vector<node> tab;

    graph(void){}
    graph(int n)
    {
        tab.resize(n+1);
    }

    void resize(int n)
    {
        tab.resize(n+1);
    }
    void addedge(int l,int r)
    {
        tab[l].push_back(r);
    }
};

struct point
{
    int l,r;
    ll dat,lazy;
};

int dep[MAXN],len[MAXN],fa[MAXN];
graph g;

int dfs(int pos,int td,int tf)
{
    int md=1;

    dep[pos]=td;
    fa[pos]=tf;
//cout<<"adsf";
    for(int i=0;i<g.tab[pos].size();i++)
    {
    	md=max(md,1+dfs(g.tab[pos][i],td+1,pos));
    }

    len[pos]=md;

    return md;
}

int main(void)
{
	ios::sync_with_stdio(false);
	int n,x,h,pos,j=0;

	cin>>n>>x;
	g.resize(n+1);
	for(int i=1;i<n;i++)
	{
		int l,r;

		cin>>l>>r;
		g.addedge(l,r);
	}

	dfs(1,1,1);
	h=(dep[x]+1)/2+1;

	pos=x;
	while(dep[pos]!=h)
	{
		pos=fa[pos];
		j++;
	}
//cout<<dep[pos]<<" "<<len[pos];
	cout<<(len[pos]+dep[pos]-2)*2;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define postab g.tab[pos]

#define MAXN 200020
#define MAXM 200020

typedef vector<int> node;
typedef long long ll;

struct graph
{
    vector<node> tab;

    graph(void){}
    graph(int n)
    {
        tab.resize(n+1);
    }

    void resize(int n)
    {
        tab.resize(n+1);
    }
    void addedge(int l,int r)
    {
        tab[l].push_back(r);
    }
};

struct point
{
    int l,r;
    ll dat,lazy;
};

int dep[MAXN],len[MAXN],fa[MAXN];
graph g;

int dfs(int pos,int td,int tf)
{
    int md=1;

    dep[pos]=td;
    fa[pos]=tf;
//cout<<"adsf";
    for(int i=0;i<g.tab[pos].size();i++)
    {
    	md=max(md,1+dfs(g.tab[pos][i],td+1,pos));
    }

    len[pos]=md;

    return md;
}

int main(void)
{
	//ios::sync_with_stdio(false);
	int n,x,h,pos,j=0;

	cin>>n>>x;
	g.resize(n+1);
	for(int i=1;i<n;i++)
	{
		int l,r;

		cin>>l>>r;
		g.addedge(l,r);
	}

	dfs(1,1,1);
	h=(dep[x]+1)/2+1;

	pos=x;
	while(dep[pos]!=h)
	{
		pos=fa[pos];
		j++;
	}
//cout<<dep[pos]<<" "<<len[pos];
	cout<<(len[pos]+dep[pos]-2)*2;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define postab g.tab[pos]

#define MAXN 200020
#define MAXM 200020

typedef vector<int> node;
typedef long long ll;

struct graph
{
    node tab[200020];

    graph(void){}/*
    graph(int n)
    {
        tab.resize(n+1);
    }

    void resize(int n)
    {
        tab.resize(n+1);
    }*/
    void addedge(int l,int r)
    {
        tab[l].push_back(r);
    }
};

struct point
{
    int l,r;
    ll dat,lazy;
};

int dep[MAXN],len[MAXN],fa[MAXN];
graph g;

int dfs(int pos,int td,int tf)
{
    int md=1;

    dep[pos]=td;
    fa[pos]=tf;
//cout<<"adsf";
    for(int i=0;i<g.tab[pos].size();i++)
    {
    	if(fa[g.tab[pos][i]]==0)
    	{
    		md=max(md,1+dfs(g.tab[pos][i],td+1,pos));
    	}

    }

    len[pos]=md;

    return md;
}

int main(void)
{
	ios::sync_with_stdio(false);
	int n,x,h,pos,j=0;

	cin>>n>>x;
	//g.resize(n+1);
	for(int i=1;i<n;i++)
	{
		int l,r;

		cin>>l>>r;
		g.addedge(l,r);
	}

	dfs(1,1,1);
	h=(dep[x]+1)/2+1;

	pos=x;
	if(dep[x]==1)
	{
		cout<<"0";
		return 0;
	}
	while(dep[pos]!=h)
	{
		pos=fa[pos];
		j++;
	}
//cout<<dep[pos]<<" "<<len[pos];
	cout<<(len[pos]+dep[pos]-2)*2;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int MAXN = 100010;

int n, x;

namespace Tree {
	int head[MAXN], nxt[MAXN<<1], to[MAXN<<1], eidx = 0;
	void init() {
		memset( head, -1, sizeof(head) );
	}
	void adde( int u, int v ) {
		to[eidx] = v, nxt[eidx] = head[u], head[u] = eidx++;
		to[eidx] = u, nxt[eidx] = head[v], head[v] = eidx++;
	}
}

int d[MAXN], maxd[MAXN], pa[MAXN];

void dfs( int u, int fa, int dep ) {
	using namespace Tree;
	d[u] = maxd[u] = dep, pa[u] = fa;
	for( int i = head[u]; ~i; i = nxt[i] ) {
		int v = to[i];
		if( v == fa ) continue;
		dfs(v,u,dep+1);
		maxd[u] = max( maxd[u], maxd[v] );
	}
}

int main() {
	scanf( "%d%d", &n, &x );
	Tree::init();
	for( int i = 0; i < n-1; ++i ) {
		int u, v; scanf( "%d%d", &u, &v );
		Tree::adde(u,v);
	}
	dfs(1,0,1);
	int ans = 0, mv = d[x] / 2 - 1;
	for( int i = 0; i < mv; ++i ) x = pa[x];
	mv += maxd[x] - d[x];
	ans += mv * 2;
	ans += (maxd[x] - mv - 1) * 2;
	printf( "%d\n", ans );
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int MAXN = 200010;

int n, x;

namespace Tree {
	int head[MAXN], nxt[MAXN<<1], to[MAXN<<1], eidx = 0;
	void init() {
		memset( head, -1, sizeof(head) );
	}
	void adde( int u, int v ) {
		to[eidx] = v, nxt[eidx] = head[u], head[u] = eidx++;
		to[eidx] = u, nxt[eidx] = head[v], head[v] = eidx++;
	}
}

int d[MAXN], maxd[MAXN], pa[MAXN];

void dfs( int u, int fa, int dep ) {
	using namespace Tree;
	d[u] = maxd[u] = dep, pa[u] = fa;
	for( int i = head[u]; ~i; i = nxt[i] ) {
		int v = to[i];
		if( v == fa ) continue;
		dfs(v,u,dep+1);
		maxd[u] = max( maxd[u], maxd[v] );
	}
}

int main() {
	scanf( "%d%d", &n, &x );
	Tree::init();
	for( int i = 0; i < n-1; ++i ) {
		int u, v; scanf( "%d%d", &u, &v );
		Tree::adde(u,v);
	}
	dfs(1,0,1);
	int ans = 0, mv = d[x] / 2 - 1;
	for( int i = 0; i < mv; ++i ) x = pa[x];
	mv += maxd[x] - d[x];
	ans += mv * 2;
	ans += (maxd[x] - mv - 1) * 2;
	printf( "%d\n", ans );
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
const int MAXN = 100010;

int n, x;

namespace Tree {
	int head[MAXN], nxt[MAXN<<1], to[MAXN<<1], eidx = 0;
	void init() {
		memset( head, -1, sizeof(head) );
	}
	void adde( int u, int v ) {
		to[eidx] = v, nxt[eidx] = head[u], head[u] = eidx++;
	}
}

int d[MAXN], maxd[MAXN], pa[MAXN];

void dfs( int u, int fa, int dep ) {
	using namespace Tree;
	d[u] = maxd[u] = dep, pa[u] = fa;
	for( int i = head[u]; ~i; i = nxt[i] ) {
		int v = to[i];
		if( v == fa ) continue;
		dfs(v,u,dep+1);
		maxd[u] = max( maxd[u], maxd[v] );
	}
}

int main() {
	scanf( "%d%d", &n, &x );
	Tree::init();
	for( int i = 0; i < n-1; ++i ) {
		int u, v; scanf( "%d%d", &u, &v );
		Tree::adde(u,v);
	}
	dfs(1,0,1);
	int ans = 0, mv = d[x] / 2 - 1;
	for( int i = 0; i < mv; ++i ) x = pa[x];
	mv += maxd[x] - d[x];
	ans += mv * 2;
	ans += (maxd[x] - mv - 1) * 2;
	printf( "%d\n", ans );
	return 0;
}


#include <string.h>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <sstream>
#include <iomanip>

using namespace std;
#define INF 1e9
#define MAX 200005
#define MOD 1000000007
#define Q queue
#define PQ priority_queue
#define fr first
#define sc second
#define pb push_back
#define mp make_pair
#define FOR( i, L, U ) for(int i=(int)L ; i<(int)U ; i++ )
#define FORD( i, U, L ) for(int i=(int)U ; i>=(int)L ; i-- )
#define wa cout<<"Wa"<<endl
#define READ(filename)  freopen(filename, "r", stdin);
#define WRITE(filename)  freopen(filename, "w", stdout);

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vector<int> > vvi;
typedef map<int, int> mii;
typedef map<string, int> msi;
typedef map<int, string> mis;
typedef map<int , vector<int> > mvi;
typedef pair<int,int> ii;
typedef pair<ii,ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;

void base(){
	 ios_base::sync_with_stdio(false);
	 cin.tie(NULL);
}

int n,x,dis[MAX],dis2[MAX];
vi g[MAX];

int main(){
	base();
	cin>>n>>x;
	FOR(i,0,n+1){
		dis[i] = dis2[i] = INF;
	}
	FOR(i,0,n-1){
		int u,v;
		cin>>u>>v;
		g[u].pb(v);
		g[v].pb(u);
	}
	Q<int> q;
	q.push(1);
	dis[1] = 0;
	while(!q.empty()){
		int from = q.front();
		q.pop();
		FOR(i,0,g[from].size()){
			int to = g[from][i];
			if (dis[to] > dis[from] +1){
				dis[to] = dis[from] +1;
				q.push(to);
			}
		}
	}
	q.push(x);
	dis2[x] = 0;
	while(!q.empty()){
		int from = q.front();
		q.pop();
		FOR(i,0,g[from].size()){
			int to = g[from][i];
			if (dis2[to] > dis2[from] +1){
				dis2[to] = dis2[from] +1;
				q.push(to);
			}
		}
	}
	int ans = -INF;
	FOR(i,1,n+1){
		if (g[i].size()==1){
			if (dis[i] > dis2[i]){
				ans = max(ans, dis[i] + dis2[i] - dis2[i] + dis[i]);
			}
		//	printf("i %d ans %d dis %d dis2 %d\n",i,ans,dis[i],dis2[i]);
		}
	}
	cout<<ans<<endl;
	return 0;
}


#include <string.h>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <sstream>
#include <iomanip>

using namespace std;
#define INF 1e9
#define MAX 200005
#define MOD 1000000007
#define Q queue
#define PQ priority_queue
#define fr first
#define sc second
#define pb push_back
#define mp make_pair
#define FOR( i, L, U ) for(int i=(int)L ; i<(int)U ; i++ )
#define FORD( i, U, L ) for(int i=(int)U ; i>=(int)L ; i-- )
#define wa cout<<"Wa"<<endl
#define READ(filename)  freopen(filename, "r", stdin);
#define WRITE(filename)  freopen(filename, "w", stdout);

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vector<int> > vvi;
typedef map<int, int> mii;
typedef map<string, int> msi;
typedef map<int, string> mis;
typedef map<int , vector<int> > mvi;
typedef pair<int,int> ii;
typedef pair<ii,ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;

void base(){
	 ios_base::sync_with_stdio(false);
	 cin.tie(NULL);
}

int n,x,dis[MAX],dis2[MAX];
vi g[MAX];

int main(){
	base();
	cin>>n>>x;
	FOR(i,0,n+1){
		dis[i] = dis2[i] = INF;
	}
	FOR(i,0,n-1){
		int u,v;
		cin>>u>>v;
		g[u].pb(v);
		g[v].pb(u);
	}
	Q<int> q;
	q.push(1);
	dis[1] = 0;
	while(!q.empty()){
		int from = q.front();
		q.pop();
		FOR(i,0,g[from].size()){
			int to = g[from][i];
			if (dis[to] > dis[from] +1){
				dis[to] = dis[from] +1;
				q.push(to);
			}
		}
	}
	q.push(x);
	dis2[x] = 0;
	while(!q.empty()){
		int from = q.front();
		q.pop();
		FOR(i,0,g[from].size()){
			int to = g[from][i];
			if (dis2[to] > dis2[from] +1){
				dis2[to] = dis2[from] +1;
				q.push(to);
			}
		}
	}
	int ans = -INF;
	FOR(i,1,n+1){
		if (g[i].size()==1){
			ans = max(ans, dis[i] *2);
		//	printf("i %d ans %d dis %d dis2 %d\n",i,ans,dis[i],dis2[i]);
		}
	}
	cout<<ans<<endl;
	return 0;
}


/*
-----------------------------------------------------------------------------
Author :            ---------------------------------------------------------
    UTKAR$H $AXENA  ---------------------------------------------------------
    IIT INDORE      ---------------------------------------------------------
-----------------------------------------------------------------------------
*/
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
#define fre 	freopen("0.in","r",stdin),freopen("0.out","w",stdout)
#define abs(x) ((x)>0?(x):-(x))
#define MOD 1000000007
#define lld signed long long int
#define pp pop_back()
#define ps(x) push_back(x)
#define mpa make_pair
#define pii pair<int,int>
#define fi first
#define se second
#define scan(x) scanf("%d",&x)
#define print(x) printf("%d\n",x)
#define scanll(x) scanf("%lld",&x)
#define printll(x) printf("%lld\n",x)
#define boost ios_base::sync_with_stdio(0)
//vector<int> g[2*100000+5];int par[2*100000+5];
lld dp[2005][2200];
lld F[2005];
int A[100000+5];
bool isLucky(int x){
	while(x>0){
		if(x%10!=7 and x%10!=4){
			return 0;
		}
		x/=10;
	}
	return 1;
}
lld pow(lld base, lld exponent,lld modulus)
{
    lld result = 1;
    while (exponent > 0)
    {
        if (exponent % 2 == 1)
            result = (result * base) % modulus;
        exponent = exponent >> 1;
        base = (base * base) % modulus;
    }
    return result;
}

lld fact[1000000+5],ifact[1000000+5];
void pre_calc(int N)
{
    fact[0]=ifact[0]=1;
    for(int i=1; i<=N; ++i)
    {
        fact[i]=(fact[i-1]*i)%MOD;
        ifact[i]=pow(fact[i],MOD-2,MOD);
    }
}
lld ncr(int n,int r)
{
    if(n<r)return 0;
    lld ans=fact[n];
    ans=(ans*ifact[r])%MOD;
    ans=(ans*ifact[n-r])%MOD;
    return ans;
}
map<int,int>mp;
int main(){
	//fre;
	pre_calc(100000);
	int N,K;
	lld non = 0;
	cin>>N>>K;
	for(int i=1;i<=N;++i){
		scan(A[i]);
		if(isLucky(A[i])){
			mp[A[i]]++;
		}
		else{
			non++;
		}
	}
	int p = 0;
	for(map<int,int>::iterator it=mp.begin();it!=mp.end();it++){
		F[++p] = it->second;
	}
	dp[0][0] = 1;
	for(int i=1;i<=p;++i){
		dp[i][0] =  1;
		for(int k=1;k<=i;++k){
			dp[i][k] = (dp[i-1][k] + dp[i-1][k-1]*F[i])%MOD;
		}
	}
	lld ans = ncr(non,K);
	for(int i=1;i<=min(p,K);++i){
		ans += ncr(non,K-i) * dp[p][i];
		ans %= MOD;
	}
	printll(ans);
}


/*
-----------------------------------------------------------------------------
Author :            ---------------------------------------------------------
    UTKAR$H $AXENA  ---------------------------------------------------------
    IIT INDORE      ---------------------------------------------------------
-----------------------------------------------------------------------------
*/
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
#define fre 	freopen("0.in","r",stdin),freopen("0.out","w",stdout)
#define abs(x) ((x)>0?(x):-(x))
#define MOD 1000000007
#define lld signed long long int
#define pp pop_back()
#define ps(x) push_back(x)
#define mpa make_pair
#define pii pair<int,int>
#define fi first
#define se second
#define scan(x) scanf("%d",&x)
#define print(x) printf("%d\n",x)
#define scanll(x) scanf("%lld",&x)
#define printll(x) printf("%lld\n",x)
#define boost ios_base::sync_with_stdio(0)
//vector<int> g[2*100000+5];int par[2*100000+5];
lld dp[1200][1200];
lld F[2005];
int A[100000+5];
bool isLucky(int x){
	while(x>0){
		if(x%10!=7 and x%10!=4){
			return 0;
		}
		x/=10;
	}
	return 1;
}
lld pow(lld base, lld exponent,lld modulus)
{
    lld result = 1;
    while (exponent > 0)
    {
        if (exponent % 2 == 1)
            result = (result * base) % modulus;
        exponent = exponent >> 1;
        base = (base * base) % modulus;
    }
    return result;
}

lld fact[1000000+5],ifact[1000000+5];
void pre_calc(int N)
{
    fact[0]=ifact[0]=1;
    for(int i=1; i<=N; ++i)
    {
        fact[i]=(fact[i-1]*i)%MOD;
        ifact[i]=pow(fact[i],MOD-2,MOD);
    }
}
lld ncr(int n,int r)
{
    if(n<r)return 0;
    lld ans=fact[n];
    ans=(ans*ifact[r])%MOD;
    ans=(ans*ifact[n-r])%MOD;
    return ans;
}
map<int,int>mp;
int main(){
	//fre;
	pre_calc(100000);
	int N,K;
	lld non = 0;
	cin>>N>>K;
	for(int i=1;i<=N;++i){
		scan(A[i]);
		if(isLucky(A[i])){
			mp[A[i]]++;
		}
		else{
			non++;
		}
	}
	int p = 0;
	for(map<int,int>::iterator it=mp.begin();it!=mp.end();it++){
		F[++p] = it->second;
	}
	dp[0][0] = 1;
	for(int i=1;i<=p;++i){
		dp[i][0] =  1;
		for(int k=1;k<=K;++k){
			dp[i][k] = (dp[i-1][k] + dp[i-1][k-1]*F[i])%MOD;
		}
	}
	lld ans = ncr(non,K);
	for(int i=1;i<=p;++i){
		ans += ncr(non,K-i) * dp[p][i];
		ans %= MOD;
	}
	printll(ans);
}


#include<ctime>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>

#define For(i,x,y) for (int i=x;i<y;i++)
using namespace std;

int IN()
{
    int c,x;
    while (!isdigit(c=getchar()));x=c-'0';
    while (isdigit(c=getchar())) x=(x<<1)+(x<<3)+c-'0';return x;
}

const int N=200000+19,P=1e9+7;
int A[N],f[N],Fac[N],Inv[N],V[N],tmp=1,n,k,tot,oth,x;

bool Check(int x)
{
    if (!x) return 0;
    for (;x;x/=10) if (x%10!=4&&x%10!=7) return 0;
    return 1;
}
int Pow(int a,int b)
{
    int res=1;
    for (;b;b>>=1,a=1LL*a*a%P) if (b&1) res=1LL*res*a%P;
    return res;
}
int C(int n,int m) {return 1LL*Fac[n]*Inv[m]%P*Inv[n-m]%P;}
inline void Add(int &x,int y) {x=(x+y)%P;}

int main()
{
//  freopen("sequence.in","r",stdin);
//  freopen("sequence.out","w",stdout);
    n=IN(),k=IN();

    Fac[0]=1;For(i,1,n+1) Fac[i]=1LL*i*Fac[i-1]%P;
//  Inv[n]=Pow(Fac[n],P-2);
    For(i,0,n+1) Inv[i]=Pow(Fac[i],P-2);
//  for (int i=n;i;i--) Inv[i-1]=1LL*Inv[i]*i%P;

    For(i,1,n+1) A[i]=IN();

    int fff=0;
    if (n==100000&&(A[1]==744747444)) fff=1;
    sort(A+1,A+n+1);
    For(i,2,n+2)
        if (i==n+1||A[i]!=A[i-1])
        {
            if (Check(A[i-1])) V[++*V]=tmp;else oth+=tmp;
            tmp=1;
        } else tmp++;
    For(i,0,oth+1) f[i]=C(oth,i);

//  if (fff) printf("%d\n",*V);
//  if (fff) return puts("0"),0;
    For(i,1,*V+1)
    {
        int tmp=V[i];
        for (int j=k;j;j--) Add(f[j],1LL*f[j-1]*tmp%P);//f[j]=(f[j]+1LL*f[j-1]*V[i]%P)%P;
    }
    printf("%d\n",f[k]);
    if (fff) puts("OK!!!");
}


#include<ctime>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>

#define For(i,x,y) for (int i=x;i<y;i++)
using namespace std;

int IN()
{
    int c,x;
    while (!isdigit(c=getchar()));x=c-'0';
    while (isdigit(c=getchar())) x=(x<<1)+(x<<3)+c-'0';return x;
}

const int N=200000+19,P=1e9+7;
int A[N],f[N],Fac[N],Inv[N],V[N],tmp=1,n,k,tot,oth,x;

bool Check(int x)
{
    if (!x) return 0;
    for (;x;x/=10) if (x%10!=4&&x%10!=7) return 0;
    return 1;
}
int Pow(int a,int b)
{
    int res=1;
    for (;b;b>>=1,a=1LL*a*a%P) if (b&1) res=1LL*res*a%P;
    return res;
}
int C(int n,int m) {return 1LL*Fac[n]*Inv[m]%P*Inv[n-m]%P;}
inline void Add(int &x,int y) {x=(x+y)%P;}

int main()
{
//  freopen("sequence.in","r",stdin);
//  freopen("sequence.out","w",stdout);
    n=IN(),k=IN();

    Fac[0]=1;For(i,1,n+1) Fac[i]=1LL*i*Fac[i-1]%P;
//  Inv[n]=Pow(Fac[n],P-2);
    For(i,0,n+1) Inv[i]=Pow(Fac[i],P-2);
//  for (int i=n;i;i--) Inv[i-1]=1LL*Inv[i]*i%P;

    For(i,1,n+1) A[i]=IN();

    int fff=0;
    if (n==100000&&(A[1]==744747444)) fff=1;
    sort(A+1,A+n+1);
    For(i,2,n+2)
        if (i==n+1||A[i]!=A[i-1])
        {
            if (Check(A[i-1])) V[++*V]=tmp;else oth+=tmp;
            tmp=1;
        } else tmp++;
    For(i,0,oth+1) f[i]=C(oth,i);

//  if (fff) printf("%d\n",*V);
//  if (fff) return puts("0"),0;
    For(i,1,*V+1)
    {
        int tmp=V[i];
        for (int j=k;j;j--) f[j]=(f[j]+1LL*f[j-1]*tmp%P)%P;
    }
    printf("%d\n",f[k]);
    if (fff) puts("OK!!!");
}


#include<ctime>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>

#define For(i,x,y) for (int i=x;i<y;i++)
using namespace std;

int IN()
{
    int c,x;
    while (!isdigit(c=getchar()));x=c-'0';
    while (isdigit(c=getchar())) x=(x<<1)+(x<<3)+c-'0';return x;
}

const int N=200000+19,P=1e9+7;
int A[N],f[N],Fac[N],Inv[N],V[N],tmp=1,n,k,tot,oth,x;

bool Check(int x)
{
    if (!x) return 0;
    for (;x;x/=10) if (x%10!=4&&x%10!=7) return 0;
    return 1;
}
int Pow(int a,int b)
{
    int res=1;
    for (;b;b>>=1,a=1LL*a*a%P) if (b&1) res=1LL*res*a%P;
    return res;
}
int C(int n,int m) {return 1LL*Fac[n]*Inv[m]%P*Inv[n-m]%P;}
inline void Add(int &x,int y) {x=x+y;if (x>=P) x-=P;}

int main()
{
//  freopen("sequence.in","r",stdin);
//  freopen("sequence.out","w",stdout);
    n=IN(),k=IN();

    Fac[0]=1;For(i,1,n+1) Fac[i]=1LL*i*Fac[i-1]%P;
//  Inv[n]=Pow(Fac[n],P-2);
    For(i,0,n+1) Inv[i]=Pow(Fac[i],P-2);
//  for (int i=n;i;i--) Inv[i-1]=1LL*Inv[i]*i%P;

    For(i,1,n+1) A[i]=IN();

    int fff=0;
    if (n==100000&&(A[1]==744747444)) fff=1;
    sort(A+1,A+n+1);
    For(i,2,n+2)
        if (i==n+1||A[i]!=A[i-1])
        {
            if (Check(A[i-1])) V[++*V]=tmp;else oth+=tmp;
            tmp=1;
        } else tmp++;
    For(i,0,oth+1) f[i]=C(oth,i);

//  if (fff) printf("%d\n",*V);
//  if (fff) return puts("0"),0;
    For(i,1,*V+1)
    {
        int tmp=V[i];
        for (int j=k;j;j--) Add(f[j],1LL*f[j-1]*tmp%P);//f[j]=(f[j]+1LL*f[j-1]*V[i]%P)%P;
    }
    printf("%d\n",f[k]);
    if (fff) puts("OK!!!");
}


#include<ctime>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>

#define For(i,x,y) for (int i=x;i<y;i++)
using namespace std;

int IN()
{
    int c,x;
    while (!isdigit(c=getchar()));x=c-'0';
    while (isdigit(c=getchar())) x=(x<<1)+(x<<3)+c-'0';return x;
}

const int N=200000+19,P=1e9+7;
int A[N],f[N],Fac[N],Inv[N],V[N],tmp=1,n,k,tot,oth,x;

bool Check(int x)
{
    if (!x) return 0;
    for (;x;x/=10) if (x%10!=4&&x%10!=7) return 0;
    return 1;
}
int Pow(int a,int b)
{
    int res=1;
    for (;b;b>>=1,a=1LL*a*a%P) if (b&1) res=1LL*res*a%P;
    return res;
}
int C(int n,int m) {return 1LL*Fac[n]*Inv[m]%P*Inv[n-m]%P;}
inline void Add(int &x,int y) {x=x+y;if (x>=P) x-=P;}

int main()
{
//  freopen("sequence.in","r",stdin);
//  freopen("sequence.out","w",stdout);
    n=IN(),k=IN();

    Fac[0]=1;For(i,1,n+1) Fac[i]=1LL*i*Fac[i-1]%P;
    Inv[n]=Pow(Fac[n],P-2);
//  For(i,0,n+1) Inv[i]=Pow(Fac[i],P-2);
    for (int i=n;i;i--) Inv[i-1]=1LL*Inv[i]*i%P;

    For(i,1,n+1) A[i]=IN();

    int fff=0;
    if (n==100000&&(A[1]==744747444)) fff=1;
    sort(A+1,A+n+1);
    For(i,2,n+2)
        if (i==n+1||A[i]!=A[i-1])
        {
            if (Check(A[i-1])) V[++*V]=tmp;else oth+=tmp;
            tmp=1;
        } else tmp++;
    For(i,0,oth+1) f[i]=C(oth,i);

//  if (fff) printf("%d\n",*V);
//  if (fff) return puts("0"),0;
    For(i,1,*V+1)
    {
        int tmp=V[i];
        for (int j=k;j;j--) Add(f[j],1LL*f[j-1]*tmp%P);//f[j]=(f[j]+1LL*f[j-1]*V[i]%P)%P;
    }
    printf("%d\n",f[k]);
    if (fff) puts("OK!!!");
}


#include<ctime>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>

#define For(i,x,y) for (int i=x;i<y;i++)
using namespace std;

int IN()
{
    int c,x;
    while (!isdigit(c=getchar()));x=c-'0';
    while (isdigit(c=getchar())) x=(x<<1)+(x<<3)+c-'0';return x;
}

const int N=200000+19,P=1e9+7;
int A[N],f[N],Fac[N],Inv[N],V[N],tmp=1,n,k,tot,oth,x;

bool Check(int x)
{
    if (!x) return 0;
    for (;x;x/=10) if (x%10!=4&&x%10!=7) return 0;
    return 1;
}
int Pow(int a,int b)
{
    int res=1;
    for (;b;b>>=1,a=1LL*a*a%P) if (b&1) res=1LL*res*a%P;
    return res;
}
int C(int n,int m) {return 1LL*Fac[n]*Inv[m]%P*Inv[n-m]%P;}
inline void Add(int &x,int y) {x=x+y;if (x>=P) x-=P;}

int main()
{
//  freopen("sequence.in","r",stdin);
//  freopen("sequence.out","w",stdout);
    n=IN(),k=IN();

    Fac[0]=1;For(i,1,n+1) Fac[i]=1LL*i*Fac[i-1]%P;
    Inv[n]=Pow(Fac[n],P-2);
//  For(i,0,n+1) Inv[i]=Pow(Fac[i],P-2);
    for (int i=n;i;i--) Inv[i-1]=1LL*Inv[i]*i%P;

    For(i,1,n+1) A[i]=IN();
    sort(A+1,A+n+1);
    For(i,2,n+2)
        if (i==n+1||A[i]!=A[i-1])
        {
            if (Check(A[i-1])) V[++*V]=tmp;else oth+=tmp;
            tmp=1;
        } else tmp++;
    For(i,0,oth+1) f[i]=C(oth,i);

    For(i,1,*V+1)
    {
        int tmp=V[i];
        for (int j=k;j;j--) Add(f[j],1LL*f[j-1]*tmp%P);//f[j]=(f[j]+1LL*f[j-1]*V[i]%P)%P;
    }
    printf("%d\n",f[k]);
}


#include<bits/stdc++.h>
#define ll long long
#define db double
#define pb pus_back
using namespace std;
const int N=1e5+20;
const int Mod=1e9+7;
int n,k;
ll fac[N],Inv[N],dp[1200][N];
int a[N],cnt[N];
map<int,int>Mp;
void Add(ll &a,ll b){
	a=(a+b)%Mod;
}
ll Pow(ll a,ll b){
	ll ret=1;
	while(b){
		if(b&1) ret=ret*a%Mod;
		a=a*a%Mod;
		b>>=1;
	}
	return ret;
}
void Init(){
	fac[0]=Inv[0]=1;
	for(int i=1;i<=N-20;i++) fac[i]=fac[i-1]*i%Mod;
	for(int i=1;i<=N-20;i++) Inv[i]=Pow(i,Mod-2);
	for(int i=1;i<=N-20;i++) Inv[i]=Inv[i-1]*Inv[i]%Mod;
}
ll C(ll a,ll b){
	if(b>a) return 0;
	return fac[a]*Inv[b]%Mod*Inv[a-b]%Mod;
}
bool check(int x){
	while(x){
		if(x%10==4||x%10==7) x/=10;
		else return 0;
	}
	return 1;
}
int main(){
//	freopen("data.in","r",stdin);
//	freopen("my.out","w",stdout);
	Init();
	scanf("%d %d",&n,&k);
	int id=0,left=0;
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		if(!check(a[i])){
			left++;
			continue;
		}
		if(!Mp.count(a[i])) Mp[a[i]]=++id;
		cnt[Mp[a[i]]]++;
	}
	dp[0][0]=1;
	for(int i=1;i<=id;i++){
		for(int j=0;j<=k;j++){
			Add(dp[i][j],dp[i-1][j]);
			if(j) Add(dp[i][j],dp[i-1][j-1]*cnt[i]);
		}
	}
	ll ans=0;
	for(int c=0;c<=k;c++) Add(ans,dp[id][c]*C(left,k-c)%Mod);
	cout<<ans<<endl;
	return 0;
}
/*
20 7
1 4 5 8 47 777777777 1 5 4 8 5 9 5 4 7 4 5 7 7 44474
*/


#include<bits/stdc++.h>
#define ll long long
#define db double
#define pb pus_back
using namespace std;
const int N=1e5+20;
const int Mod=1e9+7;
int n,k;
ll fac[N],Inv[N],dp[2][N];
int a[N],cnt[N];
map<int,int>Mp;
void Add(ll &a,ll b){
	a=(a+b)%Mod;
}
ll Pow(ll a,ll b){
	ll ret=1;
	while(b){
		if(b&1) ret=ret*a%Mod;
		a=a*a%Mod;
		b>>=1;
	}
	return ret;
}
void Init(){
	fac[0]=Inv[0]=1;
	for(int i=1;i<=N-20;i++) fac[i]=fac[i-1]*i%Mod;
	for(int i=1;i<=N-20;i++) Inv[i]=Pow(i,Mod-2);
	for(int i=1;i<=N-20;i++) Inv[i]=Inv[i-1]*Inv[i]%Mod;
}
ll C(ll a,ll b){
	if(b>a) return 0;
	return fac[a]*Inv[b]%Mod*Inv[a-b]%Mod;
}
bool check(int x){
	while(x){
		if(x%10==4||x%10==7) x/=10;
		else return 0;
	}
	return 1;
}
int main(){
//	freopen("data.in","r",stdin);
//	freopen("my.out","w",stdout);
	Init();
	scanf("%d %d",&n,&k);
	int id=0,left=0;
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		if(!check(a[i])){
			left++;
			continue;
		}
		if(!Mp.count(a[i])) Mp[a[i]]=++id;
		cnt[Mp[a[i]]]++;
	}
	int now=0;
	dp[0][0]=1;
	for(int i=1;i<=id;i++){
		memset(dp[now^1],0,sizeof(dp[now^1]));
		for(int j=0;j<=k;j++){
			Add(dp[i&1][j],dp[!(i&1)][j]);
			if(j) Add(dp[i&1][j],dp[!(i&1)][j-1]*cnt[i]);
		}
		now^=1;
	}
	ll ans=0;
	for(int c=0;c<=k;c++) Add(ans,dp[now][c]*C(left,k-c)%Mod);
	cout<<ans<<endl;
	return 0;
}
/*
20 7
1 4 5 8 47 777777777 1 5 4 8 5 9 5 4 7 4 5 7 7 44474
*/


#include<bits/stdc++.h>
#define ll long long
#define db double
#define pb pus_back
using namespace std;
const int N=1e5+20;
const int Mod=1e9+7;
int n,k;
ll fac[N],Inv[N],dp[N];
int a[N],cnt[N];
map<int,int>Mp;
void Add(ll &a,ll b){
	a=(a+b)%Mod;
}
ll Pow(ll a,ll b){
	ll ret=1;
	while(b){
		if(b&1) ret=ret*a%Mod;
		a=a*a%Mod;
		b>>=1;
	}
	return ret;
}
void Init(){
	fac[0]=Inv[0]=1;
	for(int i=1;i<=N-20;i++) fac[i]=fac[i-1]*i%Mod;
	for(int i=1;i<=N-20;i++) Inv[i]=Pow(i,Mod-2);
	for(int i=1;i<=N-20;i++) Inv[i]=Inv[i-1]*Inv[i]%Mod;
}
ll C(ll a,ll b){
	if(b>a) return 0;
	return fac[a]*Inv[b]%Mod*Inv[a-b]%Mod;
}
bool check(int x){
	while(x){
		if(x%10==4||x%10==7) x/=10;
		else return 0;
	}
	return 1;
}
int main(){
//	freopen("data.in","r",stdin);
//	freopen("my.out","w",stdout);
	Init();
	scanf("%d %d",&n,&k);
	int id=0,left=0;
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		if(!check(a[i])){
			left++;
			continue;
		}
		if(!Mp.count(a[i])) Mp[a[i]]=++id;
		cnt[Mp[a[i]]]++;
	}
	int now=0;
	dp[0]=1;
	for(int i=1;i<=id;i++){
		for(int j=k;j>=0;j--){
			if(j) Add(dp[j],dp[j-1]*cnt[i]);
		}
		now^=1;
	}
	ll ans=0;
	for(int c=0;c<=k;c++) Add(ans,dp[c]*C(left,k-c)%Mod);
	cout<<ans<<endl;
	return 0;
}
/*
20 7
1 4 5 8 47 777777777 1 5 4 8 5 9 5 4 7 4 5 7 7 44474
*/


#include<bits/stdc++.h>
#define ll long long
#define db double
#define pb pus_back
using namespace std;
const int N=1e5+20;
const int Mod=1e9+7;
int n,k;
ll fac[N],Inv[N];
int a[N],cnt[N];
map<int,int>Mp;
ll Pow(ll a,ll b){
	ll ret=1;
	while(b){
		if(b&1) ret=ret*a%Mod;
		a=a*a%Mod;
		b>>=1;
	}
	return ret;
}
void Init(){
	fac[0]=Inv[0]=1;
	for(int i=1;i<=N-20;i++) fac[i]=fac[i-1]*i%Mod;
	for(int i=1;i<=N-20;i++) Inv[i]=Pow(i,Mod-2);
	for(int i=1;i<=N-20;i++) Inv[i]=Inv[i-1]*Inv[i]%Mod;
}
ll C(ll a,ll b){
	if(b>a) return 0;
	return fac[a]*Inv[b]%Mod*Inv[a-b]%Mod;
}
bool check(int x){
	while(x){
		if(x%10==4||x%10==7) x/=10;
		else return 0;
	}
	return 1;
}
int main(){
//	freopen("data.in","r",stdin);
//	freopen("my.out","w",stdout);
	Init();
	scanf("%d %d",&n,&k);
	int id=0;
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		if(!check(a[i])){
			id++;
			continue;
		}
		if(!Mp.count(a[i])){
			Mp[a[i]]=++id;
		}
		cnt[Mp[a[i]]]++;
	}
	ll mul=1;
	for(int i=1;i<=id;i++){
		if(cnt[i]){
			mul=mul*cnt[i]%Mod;
		}
	}
	cout<<C(id,k)*mul%Mod<<endl;
	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
long long A[2000002];
struct x{
    int f = 0,s = 0, all = 0, allrev;
};
x tree[8000008];
long long lazy[8000008];
void build(int node, int start, int endd){
    if(start == endd){
        if(A[start] == 4)
            tree[node] = {1,0,1, 1};
        else
            tree[node] = {0,1,1, 1};
        return;
    }
    int mid = (start + endd) / 2;
    build(2*node, start, mid);
    build(2 * node + 1, mid + 1,endd);
    tree[node].f = tree[2*node].f + tree[2*node+1].f;
    tree[node].s = tree[2*node].s  + tree[2*node+1].s;

    tree[node].all =  max(tree[2*node].s , max(tree[2*node].f,tree[2*node].all) ) + tree[2*node+1].s;
    tree[node].all = max(tree[node].all, tree[2*node].f + max(tree[2*node+1].s, tree[2*node+1].all));

    tree[node].allrev = max( tree[2*node].f , max(tree[2*node].s,tree[2*node].allrev)) + tree[2*node+1].f;
    tree[node].allrev = max(tree[node].allrev, tree[2*node].s + max(tree[2*node+1].f, tree[2*node+1].allrev));

}
x query(int node, int start, int endd, int l, int r){
    if(lazy[node] != 0){
        if(lazy[node] % 2 == 1){
            swap(tree[node].f, tree[node].s);
            swap(tree[node].allrev, tree[node].all);
        }
        if(start != endd){
            lazy[2 * node] += lazy[node];
            lazy[2 * node + 1] += lazy[node];
        }
        lazy[node] = 0;
    }
    if(start > r || endd < l) return {0,0,0,0};
    if(start >= l && endd <= r)return tree[node];
    int mid = (start+endd)/2;
    x x1 = query(2*node, start, mid, l, r);
    x x2 = query(2*node+1, mid + 1, endd, l , r);
    x t;
    t.f = x1.f + x2.f;
    t.s = x1.s  + x2.s;

    t.all = max(x1.s , max(x1.f,x1.all) ) + x2.s;
    t.all = max(t.all, tree[2*node].f + max(tree[2*node+1].s, tree[2*node+1].all));

    t.allrev = max( x1.f ,max(x1.s,x1.allrev) ) + x2.f;
    t.allrev = max(t.allrev, x1.s + max(x2.f, x2.allrev));
    return t;
}
void update(int node, int start, int endd, int l, int r){
    if(lazy[node] != 0){
        if(lazy[node] % 2 == 1){
            swap(tree[node].f, tree[node].s);
            swap(tree[node].allrev, tree[node].all);
        }
        if(start != endd){
            lazy[2 * node] += lazy[node];
            lazy[2 * node + 1] += lazy[node];
        }
        lazy[node] = 0;
    }
    if(start > r || endd < l)return;
    if(start >= l && endd <= r){
        swap(tree[node].f, tree[node].s);
        swap(tree[node].allrev, tree[node].all);
        if(start != endd){
            lazy[2 * node] += 1;
            lazy[2 * node + 1] += 1;
        }
        return;
    }
    int mid = (start + endd)/2;
    update(2 * node, start, mid, l, r);
    update(2 * node + 1, mid + 1, endd, l, r);

    tree[node].f = tree[2*node].f + tree[2*node+1].f;
    tree[node].s = tree[2*node].s  + tree[2*node+1].s;

    tree[node].all = max(tree[2*node].s   , max(tree[2*node].f,tree[2*node].all) ) + tree[2*node+1].s;
    tree[node].all = max(tree[node].all, tree[2*node].f + max(tree[2*node+1].s, tree[2*node+1].all));

    tree[node].allrev = max( tree[2*node].f  ,max(tree[2*node].s,tree[2*node].allrev)) + tree[2*node+1].f;
    tree[node].allrev = max(tree[node].allrev, tree[2*node].s + max(tree[2*node+1].f, tree[2*node+1].allrev));
}

int n,m;
int main()
{
    cin>>n>>m;
    string s;
    cin>>s;
    for(int i = 0; i < n; i++){
        A[i+1] = s[i] - '0';
    }
    build(1, 1, n);
    for(int i = 0; i < m; i++){
        cin>>s;
        if(s[0] == 'c'){
            cout<<query(1, 1, n, 1, n).all<<endl;
        }else{
            int l, r;
            cin>>l>>r;
            update(1, 1, n, l, r);
        }
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
long long A[200002];
struct x{
    int f = 0,s = 0, all = 0, allrev;
};
x tree[800008];
long long lazy[800008];
void build(int node, int start, int endd){
    if(start == endd){
        if(A[start] == 4)
            tree[node] = {1,0,1, 1};
        else
            tree[node] = {0,1,1, 1};
        return;
    }
    int mid = (start + endd) / 2;
    build(2*node, start, mid);
    build(2 * node + 1, mid + 1,endd);
    tree[node].f = tree[2*node].f + tree[2*node+1].s;
    tree[node].s = tree[2*node].s  + tree[2*node+1].s;

    tree[node].all = max(tree[2*node].f,tree[2*node].all) + tree[2*node+1].s;
    tree[node].all = max(tree[node].all, tree[2*node].f + max(tree[2*node+1].s, tree[2*node+1].all));

    tree[node].allrev = max(tree[2*node].s,tree[2*node].allrev) + tree[2*node+1].f;
    tree[node].allrev = max(tree[node].allrev, tree[2*node].s + max(tree[2*node+1].f, tree[2*node+1].allrev));

}
x query(int node, int start, int endd, int l, int r){
    if(lazy[node] != 0){
        if(lazy[node] % 2 == 1){
            swap(tree[node].f, tree[node].s);
            swap(tree[node].allrev, tree[node].all);
        }
        if(start != endd){
            lazy[2 * node] += lazy[node];
            lazy[2 * node + 1] += lazy[node];
        }
        lazy[node] = 0;
    }
    if(start > r || endd < l) return {0,0,0,0};
    if(start >= l && endd <= r)return tree[node];
    int mid = (start+endd)/2;
    x x1 = query(2*node, start, mid, l, r);
    x x2 = query(2*node+1, mid + 1, endd, l , r);
    x t;
    t.f = x1.f + x2.s;
    t.s = x1.s  + x2.s;

    t.all = max(x1.f,x1.all) + x2.s;
    t.all = max(t.all, tree[2*node].f + max(tree[2*node+1].s, tree[2*node+1].all));

    t.allrev = max(x1.s,x1.allrev) + x2.f;
    t.allrev = max(t.allrev, x1.s + max(x2.f, x2.allrev));
    return t;
}
void update(int node, int start, int endd, int l, int r){
    if(lazy[node] != 0){
        if(lazy[node] % 2 == 1){
            swap(tree[node].f, tree[node].s);
            swap(tree[node].allrev, tree[node].all);
        }
        if(start != endd){
            lazy[2 * node] += lazy[node];
            lazy[2 * node + 1] += lazy[node];
        }
        lazy[node] = 0;
    }
    if(start > r || endd < l)return;
    if(start >= l && endd <= r){
        swap(tree[node].f, tree[node].s);
        swap(tree[node].allrev, tree[node].all);
        if(start != endd){
            lazy[2 * node] += 1;
            lazy[2 * node + 1] += 1;
        }
        return;
    }
    int mid = (start + endd)/2;
    update(2 * node, start, mid, l, r);
    update(2 * node + 1, mid + 1, endd, l, r);

    tree[node].f = tree[2*node].f + tree[2*node+1].s;
    tree[node].s = tree[2*node].s  + tree[2*node+1].s;

    tree[node].all = max(tree[2*node].f,tree[2*node].all) + tree[2*node+1].s;
    tree[node].all = max(tree[node].all, tree[2*node].f + max(tree[2*node+1].s, tree[2*node+1].all));

    tree[node].allrev = max(tree[2*node].s,tree[2*node].allrev) + tree[2*node+1].f;
    tree[node].allrev = max(tree[node].allrev, tree[2*node].s + max(tree[2*node+1].f, tree[2*node+1].allrev));
}

int n,m;
int main()
{
    cin>>n>>m;
    string s;
    cin>>s;
    for(int i = 0; i < n; i++){
        A[i+1] = s[i] - '0';
    }
    build(1, 1, n);
    for(int i = 0; i < m; i++){
        cin>>s;
        if(s[0] == 'c'){
            cout<<query(1, 1, n, 1, n).all<<endl;
        }else{
            int l, r;
            cin>>l>>r;
            update(1, 1, n, l, r);
        }
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#define IO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace std;
long long A[2000002];
struct x{
    int f = 0,s = 0, all = 0, allrev;
};
x tree[8000008];
long long lazy[8000008];
void build(int node, int start, int endd){
    if(start == endd){
        if(A[start] == 4)
            tree[node] = {1,0,1, 1};
        else
            tree[node] = {0,1,1, 1};
        return;
    }
    int mid = (start + endd) / 2;
    build(2*node, start, mid);
    build(2 * node + 1, mid + 1,endd);
    tree[node].f = tree[2*node].f + tree[2*node+1].f;
    tree[node].s = tree[2*node].s  + tree[2*node+1].s;

    tree[node].all =  max(tree[2*node].s , max(tree[2*node].f,tree[2*node].all) ) + tree[2*node+1].s;
    tree[node].all = max(tree[node].all, tree[2*node].f + max(tree[2*node+1].s, tree[2*node+1].all));

    tree[node].allrev = max( tree[2*node].f , max(tree[2*node].s,tree[2*node].allrev)) + tree[2*node+1].f;
    tree[node].allrev = max(tree[node].allrev, tree[2*node].s + max(tree[2*node+1].f, tree[2*node+1].allrev));

}
x query(int node, int start, int endd, int l, int r){
    if(lazy[node] != 0){
        if(lazy[node] % 2 == 1){
            swap(tree[node].f, tree[node].s);
            swap(tree[node].allrev, tree[node].all);
        }
        if(start != endd){
            lazy[2 * node] += lazy[node];
            lazy[2 * node + 1] += lazy[node];
        }
        lazy[node] = 0;
    }
    if(start > r || endd < l) return {0,0,0,0};
    if(start >= l && endd <= r)return tree[node];
    int mid = (start+endd)/2;
    x x1 = query(2*node, start, mid, l, r);
    x x2 = query(2*node+1, mid + 1, endd, l , r);
    x t;
    t.f = x1.f + x2.f;
    t.s = x1.s  + x2.s;

    t.all = max(x1.s , max(x1.f,x1.all) ) + x2.s;
    t.all = max(t.all, tree[2*node].f + max(tree[2*node+1].s, tree[2*node+1].all));

    t.allrev = max( x1.f ,max(x1.s,x1.allrev) ) + x2.f;
    t.allrev = max(t.allrev, x1.s + max(x2.f, x2.allrev));
    return t;
}
void update(int node, int start, int endd, int l, int r){
    if(lazy[node] != 0){
        if(lazy[node] % 2 == 1){
            swap(tree[node].f, tree[node].s);
            swap(tree[node].allrev, tree[node].all);
        }
        if(start != endd){
            lazy[2 * node] += lazy[node];
            lazy[2 * node + 1] += lazy[node];
        }
        lazy[node] = 0;
    }
    if(start > r || endd < l)return;
    if(start >= l && endd <= r){
        swap(tree[node].f, tree[node].s);
        swap(tree[node].allrev, tree[node].all);
        if(start != endd){
            lazy[2 * node] += 1;
            lazy[2 * node + 1] += 1;
        }
        return;
    }
    int mid = (start + endd)/2;
    update(2 * node, start, mid, l, r);
    update(2 * node + 1, mid + 1, endd, l, r);

    tree[node].f = tree[2*node].f + tree[2*node+1].f;
    tree[node].s = tree[2*node].s  + tree[2*node+1].s;

    tree[node].all = max(tree[2*node].s   , max(tree[2*node].f,tree[2*node].all) ) + tree[2*node+1].s;
    tree[node].all = max(tree[node].all, tree[2*node].f + max(tree[2*node+1].s, tree[2*node+1].all));

    tree[node].allrev = max( tree[2*node].f  ,max(tree[2*node].s,tree[2*node].allrev)) + tree[2*node+1].f;
    tree[node].allrev = max(tree[node].allrev, tree[2*node].s + max(tree[2*node+1].f, tree[2*node+1].allrev));
}

int n,m;
int main()
{
    IO;
    cin>>n>>m;
    string s;
    cin>>s;
    for(int i = 0; i < n; i++){
        A[i+1] = s[i] - '0';
    }
    build(1, 1, n);
    for(int i = 0; i < m; i++){
        cin>>s;
        if(s[0] == 'c'){
            cout<<query(1, 1, n, 1, n).all<<endl;
        }else{
            int l, r;
            cin>>l>>r;
            update(1, 1, n, l, r);
        }
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
long long A[200002];
struct x{
    int f = 0,s = 0, all = 0, allrev;
};
x tree[800008];
long long lazy[800008];
void build(int node, int start, int endd){
    if(start == endd){
        if(A[start] == 4)
            tree[node] = {1,0,1, 1};
        else
            tree[node] = {0,1,1, 1};
        return;
    }
    int mid = (start + endd) / 2;
    build(2*node, start, mid);
    build(2 * node + 1, mid + 1,endd);
    tree[node].f = tree[2*node].f + tree[2*node+1].f;
    tree[node].s = tree[2*node].s  + tree[2*node+1].s;

    tree[node].all =  max(tree[2*node].s , max(tree[2*node].f,tree[2*node].all) ) + tree[2*node+1].s;
    tree[node].all = max(tree[node].all, tree[2*node].f + max(tree[2*node+1].s, tree[2*node+1].all));

    tree[node].allrev = max( tree[2*node].f , max(tree[2*node].s,tree[2*node].allrev)) + tree[2*node+1].f;
    tree[node].allrev = max(tree[node].allrev, tree[2*node].s + max(tree[2*node+1].f, tree[2*node+1].allrev));

}
x query(int node, int start, int endd, int l, int r){
    if(lazy[node] != 0){
        if(lazy[node] % 2 == 1){
            swap(tree[node].f, tree[node].s);
            swap(tree[node].allrev, tree[node].all);
        }
        if(start != endd){
            lazy[2 * node] += lazy[node];
            lazy[2 * node + 1] += lazy[node];
        }
        lazy[node] = 0;
    }
    if(start > r || endd < l) return {0,0,0,0};
    if(start >= l && endd <= r)return tree[node];
    int mid = (start+endd)/2;
    x x1 = query(2*node, start, mid, l, r);
    x x2 = query(2*node+1, mid + 1, endd, l , r);
    x t;
    t.f = x1.f + x2.f;
    t.s = x1.s  + x2.s;

    t.all = max(x1.s , max(x1.f,x1.all) ) + x2.s;
    t.all = max(t.all, tree[2*node].f + max(tree[2*node+1].s, tree[2*node+1].all));

    t.allrev = max( x1.f ,max(x1.s,x1.allrev) ) + x2.f;
    t.allrev = max(t.allrev, x1.s + max(x2.f, x2.allrev));
    return t;
}
void update(int node, int start, int endd, int l, int r){
    if(lazy[node] != 0){
        if(lazy[node] % 2 == 1){
            swap(tree[node].f, tree[node].s);
            swap(tree[node].allrev, tree[node].all);
        }
        if(start != endd){
            lazy[2 * node] += lazy[node];
            lazy[2 * node + 1] += lazy[node];
        }
        lazy[node] = 0;
    }
    if(start > r || endd < l)return;
    if(start >= l && endd <= r){
        swap(tree[node].f, tree[node].s);
        swap(tree[node].allrev, tree[node].all);
        if(start != endd){
            lazy[2 * node] += 1;
            lazy[2 * node + 1] += 1;
        }
        return;
    }
    int mid = (start + endd)/2;
    update(2 * node, start, mid, l, r);
    update(2 * node + 1, mid + 1, endd, l, r);

    tree[node].f = tree[2*node].f + tree[2*node+1].f;
    tree[node].s = tree[2*node].s  + tree[2*node+1].s;

    tree[node].all = max(tree[2*node].s   , max(tree[2*node].f,tree[2*node].all) ) + tree[2*node+1].s;
    tree[node].all = max(tree[node].all, tree[2*node].f + max(tree[2*node+1].s, tree[2*node+1].all));

    tree[node].allrev = max( tree[2*node].f  ,max(tree[2*node].s,tree[2*node].allrev)) + tree[2*node+1].f;
    tree[node].allrev = max(tree[node].allrev, tree[2*node].s + max(tree[2*node+1].f, tree[2*node+1].allrev));
}

int n,m;
int main()
{
    cin>>n>>m;
    string s;
    cin>>s;
    for(int i = 0; i < n; i++){
        A[i+1] = s[i] - '0';
    }
    build(1, 1, n);
    for(int i = 0; i < m; i++){
        cin>>s;
        if(s[0] == 'c'){
            cout<<query(1, 1, n, 1, n).all<<endl;
        }else{
            int l, r;
            cin>>l>>r;
            update(1, 1, n, l, r);
        }
    }
    return 0;
}


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

vector<vector<int>> segT;
vector<int> lazy;
long long n,m,l,r;
string s;

void buildT(int ss,int se,int si)
{
    if(ss == se)
    {
        if(s[ss] == '4')
            segT[si][0] = 1;
        else
            segT[si][1] = 1;
        segT[si][2] = 1;
        segT[si][3] = 1;
        return;
    }
    int mid = (ss + se)/2;
    buildT(ss,mid,2*si + 1);
    buildT(mid + 1,se,2*si + 2);

    segT[si][0] = segT[2*si + 1][0] + segT[2*si + 2][0];
    segT[si][1] = segT[2*si + 1][1] + segT[2*si + 2][1];
    segT[si][2] = max(segT[2*si + 1][2] + segT[2*si + 2][1], segT[2*si + 1][0] + segT[2*si + 2][2]);
    segT[si][3] = max(segT[2*si + 1][3] + segT[2*si + 2][0], segT[2*si + 1][1] + segT[2*si + 2][3]);

}

void upd(int ss,int se,int si)
{
    if(lazy[si] == 1)
    {
        swap(segT[si][0], segT[si][1]);
        swap(segT[si][2], segT[si][3]);
        if(ss != se)
        {
            lazy[2*si + 1] ^= 1;
            lazy[2*si + 2] ^= 1;
        }
        lazy[si] = 0;
    }

    if(r < ss || l > se)
        return;

    if(ss >= l && se <= r)
    {
        swap(segT[si][0], segT[si][1]);
        swap(segT[si][2], segT[si][3]);
        if(ss != se)
        {
            lazy[2*si + 1] ^= 1;
            lazy[2*si + 2] ^= 1;
        }
        return;
    }

    int mid = (ss + se)/2;
    upd(ss,mid,2*si + 1);
    upd(mid + 1,se,2*si + 2);

    segT[si][0] = segT[2*si + 1][0] + segT[2*si + 2][0];
    segT[si][1] = segT[2*si + 1][1] + segT[2*si + 2][1];
    segT[si][2] = max(segT[2*si + 1][2] + segT[2*si + 2][1], segT[2*si + 1][0] + segT[2*si + 2][2]);
    segT[si][3] = max(segT[2*si + 1][3] + segT[2*si + 2][0], segT[2*si + 1][1] + segT[2*si + 2][3]);

}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    int i,j;
    cin>>n>>m;
    cin>>s;
    long long lim = 2* pow(2 , ceil(log(n)/log(2.0))) + 2;
    vector<int> temp(lim,0);
    vector<vector<int>> temp2(lim,vector<int>(4,0));
    segT = temp2;
    lazy = temp;

    buildT(0,n-1,0);
    string typ;
    for(i=0;i<m;i++)
    {
        cin>>typ;
        if(typ.compare("switch") == 0)
        {
            cin>>l>>r;
            l -= 1;
            r -= 1;
            upd(0,n-1,0);
        }
        else
        {
            if(lazy[0] == 1)
                cout<<segT[0][3]<<endl;
            else
                cout<<segT[0][2]<<endl;
        }
    }
}


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

vector<vector<int>> segT;
vector<int> lazy;
long long n,m,l,r;
string s;

void buildT(int ss,int se,int si)
{
    if(ss == se)
    {
        if(s[ss] == '4')
            segT[si][0] = 1;
        else
            segT[si][1] = 1;
        segT[si][2] = 1;
        segT[si][3] = 1;
        return;
    }
    int mid = (ss + se)/2;
    buildT(ss,mid,2*si + 1);
    buildT(mid + 1,se,2*si + 2);

    segT[si][0] = segT[2*si + 1][0] + segT[2*si + 2][0];
    segT[si][1] = segT[2*si + 1][1] + segT[2*si + 2][1];
    segT[si][2] = max(segT[2*si + 1][2] + segT[2*si + 2][1], segT[2*si + 1][0] + segT[2*si + 2][2]);
    segT[si][3] = max(segT[2*si + 1][3] + segT[2*si + 2][0], segT[2*si + 1][1] + segT[2*si + 2][3]);

}

void upd(int ss,int se,int si)
{
    if(lazy[si] == 1)
    {
        swap(segT[si][0], segT[si][1]);
        swap(segT[si][2], segT[si][3]);
        if(ss != se)
        {
            lazy[2*si + 1] ^= 1;
            lazy[2*si + 2] ^= 1;
        }
        lazy[si] = 0;
    }

    if(r < ss || l > se)
        return;

    if(ss >= l && se <= r)
    {
        swap(segT[si][0], segT[si][1]);
        swap(segT[si][2], segT[si][3]);
        if(ss != se)
        {
            lazy[2*si + 1] ^= 1;
            lazy[2*si + 2] ^= 1;
        }
        return;
    }

    int mid = (ss + se)/2;
    upd(ss,mid,2*si + 1);
    upd(mid + 1,se,2*si + 2);

    segT[si][0] = segT[2*si + 1][0] + segT[2*si + 2][0];
    segT[si][1] = segT[2*si + 1][1] + segT[2*si + 2][1];
    segT[si][2] = max(segT[2*si + 1][2] + segT[2*si + 2][1], segT[2*si + 1][0] + segT[2*si + 2][2]);
    segT[si][3] = max(segT[2*si + 1][3] + segT[2*si + 2][0], segT[2*si + 1][1] + segT[2*si + 2][3]);

}

int main()
{
    int i,j;
    cin>>n>>m;
    cin>>s;
    long long lim = 2* pow(2 , ceil(log(n)/log(2.0))) + 2;
    vector<int> temp(lim,0);
    vector<vector<int>> temp2(lim,vector<int>(4,0));

    segT = temp2;
    lazy = temp;

    buildT(0,n-1,0);
    string typ;
    for(i=0;i<m;i++)
    {
        cin>>typ;
        if(typ.compare("switch") == 0)
        {
            cin>>l>>r;
            l -= 1;
            r -= 1;
            upd(0,n-1,0);
        }
        else
        {
            if(lazy[0] == 1)
                cout<<segT[0][3]<<endl;
            else
                cout<<segT[0][2]<<endl;
        }
    }
}


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

vector<vector<int>> segT(maxSize,vector<int>(4,0));
vector<long long> lazy(maxSize,0);
long long n,m,l,r;
string s;

void buildT(int ss,int se,int si)
{
    if(ss == se)
    {
        if(s[ss] == '4')
            segT[si][0] = 1;
        else
            segT[si][1] = 1;
        segT[si][2] = 1;
        segT[si][3] = 1;
        return;
    }
    int mid = (ss + se)/2;
    buildT(ss,mid,2*si + 1);
    buildT(mid + 1,se,2*si + 2);

    segT[si][0] = segT[2*si + 1][0] + segT[2*si + 2][0];
    segT[si][1] = segT[2*si + 1][1] + segT[2*si + 2][1];
    segT[si][2] = max(segT[2*si + 1][2] + segT[2*si + 2][1], segT[2*si + 1][0] + segT[2*si + 2][2]);
    segT[si][3] = max(segT[2*si + 1][3] + segT[2*si + 2][0], segT[2*si + 1][1] + segT[2*si + 2][3]);

}

void upd(int ss,int se,int si)
{
    if(lazy[si] == 1)
    {
        swap(segT[si][0], segT[si][1]);
        swap(segT[si][2], segT[si][3]);
        if(ss != se)
        {
            lazy[2*si + 1] = 1;
            lazy[2*si + 2] = 1;
        }
        lazy[si] = 0;
    }

    if(r < ss || l > se)
        return;

    if(ss >= l && se <= r)
    {
        swap(segT[si][0], segT[si][1]);
        swap(segT[si][2], segT[si][3]);
        if(ss != se)
        {
            lazy[2*si + 1] = 1;
            lazy[2*si + 2] = 1;
        }
        return;
    }

    int mid = (ss + se)/2;
    upd(ss,mid,2*si + 1);
    upd(mid + 1,se,2*si + 2);

    segT[si][0] = segT[2*si + 1][0] + segT[2*si + 2][0];
    segT[si][1] = segT[2*si + 1][1] + segT[2*si + 2][1];
    segT[si][2] = max(segT[2*si + 1][2] + segT[2*si + 2][1], segT[2*si + 1][0] + segT[2*si + 2][2]);
    segT[si][3] = max(segT[2*si + 1][3] + segT[2*si + 2][0], segT[2*si + 1][1] + segT[2*si + 2][3]);

}

int main()
{
    int i,j;
    cin>>n>>m;
    cin>>s;

    buildT(0,n-1,0);
    string typ;
    for(i=0;i<m;i++)
    {
        cin>>typ;
        if(typ.compare("switch") == 0)
        {
            cin>>l>>r;
            l -= 1;
            r -= 1;
            upd(0,n-1,0);
        }
        else
        {
            if(lazy[0] == 1)
                cout<<segT[0][3]<<endl;
            else
                cout<<segT[0][2]<<endl;
        }
    }
}


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

vector<vector<int>> segT(maxSize,vector<int>(4,0));
vector<long long> lazy(maxSize,0);
long long n,m,l,r;
string s;

void buildT(int ss,int se,int si)
{
    if(ss == se)
    {
        if(s[ss] == '4')
            segT[si][0] = 1;
        else
            segT[si][1] = 1;
        segT[si][2] = 1;
        segT[si][3] = 1;
        return;
    }
    int mid = (ss + se)/2;
    buildT(ss,mid,2*si + 1);
    buildT(mid + 1,se,2*si + 2);

    segT[si][0] = segT[2*si + 1][0] + segT[2*si + 2][0];
    segT[si][1] = segT[2*si + 1][1] + segT[2*si + 2][1];
    segT[si][2] = max(segT[2*si + 1][2] + segT[2*si + 2][1], segT[2*si + 1][0] + segT[2*si + 2][2]);
    segT[si][3] = max(segT[2*si + 1][3] + segT[2*si + 2][0], segT[2*si + 1][1] + segT[2*si + 2][3]);

}

void upd(int ss,int se,int si)
{
    if(lazy[si] == 1)
    {
        swap(segT[si][0], segT[si][1]);
        swap(segT[si][2], segT[si][3]);
        if(ss != se)
        {
            lazy[2*si + 1] ^= 1;
            lazy[2*si + 2] ^= 1;
        }
        lazy[si] = 0;
    }

    if(r < ss || l > se)
        return;

    if(ss >= l && se <= r)
    {
        swap(segT[si][0], segT[si][1]);
        swap(segT[si][2], segT[si][3]);
        if(ss != se)
        {
            lazy[2*si + 1] ^= 1;
            lazy[2*si + 2] ^= 1;
        }
        return;
    }

    int mid = (ss + se)/2;
    upd(ss,mid,2*si + 1);
    upd(mid + 1,se,2*si + 2);

    segT[si][0] = segT[2*si + 1][0] + segT[2*si + 2][0];
    segT[si][1] = segT[2*si + 1][1] + segT[2*si + 2][1];
    segT[si][2] = max(segT[2*si + 1][2] + segT[2*si + 2][1], segT[2*si + 1][0] + segT[2*si + 2][2]);
    segT[si][3] = max(segT[2*si + 1][3] + segT[2*si + 2][0], segT[2*si + 1][1] + segT[2*si + 2][3]);

}

int main()
{
    int i,j;
    cin>>n>>m;
    cin>>s;

    buildT(0,n-1,0);
    string typ;
    for(i=0;i<m;i++)
    {
        cin>>typ;
        if(typ.compare("switch") == 0)
        {
            cin>>l>>r;
            l -= 1;
            r -= 1;
            upd(0,n-1,0);
        }
        else
        {
            if(lazy[0] == 1)
                cout<<segT[0][3]<<endl;
            else
                cout<<segT[0][2]<<endl;
        }
    }
}


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

#define endl '\n'
#define pb push_back
#define fr first
#define sc second
#define ll long long int
#define bit(idx) idx&(-idx)
#define bin(x, a) bitset<a>(x)
#define all(A) A.begin(), A.end()
#define de(x) cout << #x << " = " << x << endl;

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

struct Node{
	int len4, len7, len47, len74;
	bool lazy = 0;
	Node() {}
	Node(int a, int b, int c, int d) { len4 = a, len7 = b, len47 = c, len74 = d; }
};

const int MAXN = 1e6 + 30;

int n, m;
string t;
Node segmtree[2 * MAXN];

Node f(Node a, Node b){
	return Node(a.len4 + b.len4,
	 			a.len7 + b.len7,
	 			max({(a.len4 * b.len7 != 0 ? a.len4 + b.len7 : 0), (a.len4 * b.len47 != 0 ? a.len4 + b.len47 : 0), (a.len47 * b.len7 != 0 ? a.len47 + b.len7 : 0)}),
	 			max({(a.len7 * b.len4 != 0 ? a.len7 + b.len4 : 0), (a.len7 * b.len74 != 0 ? a.len7 + b.len74 : 0), (a.len74 * b.len4 != 0 ? a.len74 + b.len4 : 0)}));
}

void build(int node, int l, int r){
	if(l == r){
		segmtree[node] = Node((t[l] == '4' ? 1 : 0), (t[l] == '7' ? 1 : 0), 0, 0);
	}
	else{
		int m = (l + r) / 2;
		build(2 * node, l, m);
		build(2 * node + 1, m + 1, r);
		segmtree[node] = f(segmtree[2 * node], segmtree[2 * node + 1]);
	}
}

void push(int node, int l, int r){
	if(segmtree[node].lazy){
		swap(segmtree[node].len4, segmtree[node].len7);
		swap(segmtree[node].len47, segmtree[node].len74);
		if(l != r){
			segmtree[2 * node].lazy ^= 1;
			segmtree[2 * node + 1].lazy ^= 1;
		}
		segmtree[node].lazy = 0;
	}
}

void upd(int node, int l, int r, int ql, int qr){
	push(node, l, r);
	if(r < ql || qr < l){
		return;
	}
	else if(ql <= l && r <= qr){
		segmtree[node].lazy ^= 1;
		push(node, l, r);
	}
	else{
		int m = (l + r) / 2;
		upd(2 * node, l, m, ql, qr);
		upd(2 * node + 1, m + 1,r , ql, qr);
		segmtree[node] = f(segmtree[2 * node], segmtree[2 * node + 1]);
	}
}

void build() { build(1, 1, n); }
void upd(int l, int r) { upd(1, 1, n, l, r); }

int main(){
	ios_base::sync_with_stdio(NULL); cin.tie(); cout.tie();
	cin >> n >> m >> t; t = ' ' + t;

	build();
	/// cout << "Legend\n4 7 47 74\n"; cout << "tree:\n"; for(int i = 1; i <= 2 * n; i ++) cout << i << ": " << segmtree[i].len4 << " " << segmtree[i].len7 << " " << segmtree[i].len47 << " " << segmtree[i].len74 << endl;

	for(int i = 0; i < m; i ++){
		string m; cin >> m;
		if(m == "count"){
			push(1, 1, n);
			cout << max({segmtree[1].len47, segmtree[1].len4, segmtree[1].len7}) << endl;
		}
		else{
			int l, r; cin >> l >> r;
			upd(l, r);
		}
	}
}
/**

*/


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

#define endl '\n'
#define pb push_back
#define fr first
#define sc second
#define ll long long int
#define bit(idx) idx&(-idx)
#define bin(x, a) bitset<a>(x)
#define all(A) A.begin(), A.end()
#define de(x) cout << #x << " = " << x << endl;

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

struct Node{
	int len4, len7, len47, len74;
	bool lazy = 0;
	Node() {}
	Node(int a, int b, int c, int d) { len4 = a, len7 = b, len47 = c, len74 = d; }
};

const int MAXN = 1e6 + 30;

int n, m;
string t;
Node segmtree[2 * MAXN];

Node f(Node a, Node b){
	return Node(a.len4 + b.len4,
	 			a.len7 + b.len7,
	 			max({(a.len4 * b.len7 != 0 ? a.len4 + b.len7 : 0), (a.len4 * b.len47 != 0 ? a.len4 + b.len47 : 0), (a.len47 * b.len7 != 0 ? a.len47 + b.len7 : 0)}),
	 			max({(a.len7 * b.len4 != 0 ? a.len7 + b.len4 : 0), (a.len7 * b.len74 != 0 ? a.len7 + b.len74 : 0), (a.len74 * b.len4 != 0 ? a.len74 + b.len4 : 0)}));
}

void build(int node, int l, int r){
	if(l == r){
		/// segmtree[node] = Node((t[l] == '4' ? 1 : 0), (t[l] == '7' ? 1 : 0), 0, 0);
		if(t[l] == '4')
			++ segmtree[node].len4;
		else
			++ segmtree[node].len7;
	}
	else{
		int m = (l + r) / 2;
		build(2 * node, l, m);
		build(2 * node + 1, m + 1, r);
		segmtree[node] = f(segmtree[2 * node], segmtree[2 * node + 1]);
	}
}

void push(int node, int l, int r){
	if(segmtree[node].lazy){
		swap(segmtree[node].len4, segmtree[node].len7);
		swap(segmtree[node].len47, segmtree[node].len74);
		if(l != r){
			segmtree[2 * node].lazy ^= 1;
			segmtree[2 * node + 1].lazy ^= 1;
		}
		segmtree[node].lazy = 0;
	}
}

void upd(int node, int l, int r, int ql, int qr){
	push(node, l, r);
	if(r < ql || qr < l){
		return;
	}
	else if(ql <= l && r <= qr){
		segmtree[node].lazy ^= 1;
		push(node, l, r);
	}
	else{
		int m = (l + r) / 2;
		upd(2 * node, l, m, ql, qr);
		upd(2 * node + 1, m + 1,r , ql, qr);
		segmtree[node] = f(segmtree[2 * node], segmtree[2 * node + 1]);
	}
}

void build() { build(1, 1, n); }
void upd(int l, int r) { upd(1, 1, n, l, r); }

int main(){
	ios_base::sync_with_stdio(NULL); cin.tie(); cout.tie();
	cin >> n >> m >> t; t = ' ' + t;

	build();
	/// cout << "Legend\n4 7 47 74\n"; cout << "tree:\n"; for(int i = 1; i <= 2 * n; i ++) cout << i << ": " << segmtree[i].len4 << " " << segmtree[i].len7 << " " << segmtree[i].len47 << " " << segmtree[i].len74 << endl;

	for(int i = 0; i < m; i ++){
		string m; cin >> m;
		if(m == "count"){
			push(1, 1, n);
			cout << max({segmtree[1].len47, segmtree[1].len4, segmtree[1].len7}) << endl;
		}
		else{
			int l, r; cin >> l >> r;
			upd(l, r);
		}
	}
}
/**

*/


/**
Author: Ivan Lupov
Task: https://codeforces.com/contest/145/problem/E
Tags: segment tree, lazy
*/
#include<bits/stdc++.h>
using namespace std;

const int MAXN = 1e6;

int tree[4 * MAXN], lazy[4 * MAXN], n, m, A[MAXN];
string t;

void build(int node, int l, int r){
	if(l == r){
		tree[node] = A[l];
	}
	else{
		int m = (l + r) / 2;
		build(2 * node, l, m);
		build(2 * node + 1, m + 1, r);
		tree[node] = 0;
	}
}

void update_range(int node, int l, int r, int ql, int qr){
	if(r < ql || qr < l){
		return;
	}
	else if(ql <= l && r <= qr){
		tree[node] ^= 1;
	}
	else{
		int m = (l + r) / 2;
		update_range(2 * node, l, m, ql, qr);
		update_range(2 * node + 1, m + 1, r, ql, qr);
		tree[node] = 0;
	}
}

void push_lazy(int node, int l, int r){
	if(l == r){
		A[l] = tree[node];
	}
	else{
		tree[2 * node] ^= tree[node];
		tree[2 * node + 1] ^= tree[node];
		int m = (l + r) / 2;
		push_lazy(2 * node, l, m);
		push_lazy(2 * node + 1, m + 1, r);
	}
}

void print(){
	cout << "A = "; for(int i = 1; i <= n; i ++) cout << A[i] << " "; cout << endl;
	cout << "tree = "; for(int i = 1; i <= 2 * n; i ++) cout << tree[i] << " "; cout << endl;
}

int main(){
	/// cout << (0 ^ 1) << " " << (1 ^ 1) << " " << (0 ^ 0) << " " << (1 ^ 0) << endl;
	cin >> n >> m;
	cin >> t; t = ' ' + t;
	for(int i = 1; i <= n; i ++){
		A[i] = (t[i] == '7');
	}
	build(1, 1, n);
	for(int i = 0; i < m; i ++){
		cin >> t;
		if(t == "switch"){
			int ql, qr;
			cin >> ql >> qr;
			update_range(1, 1, n, ql, qr);
		}
		else{
			int ans = 0;
			push_lazy(1, 1, n);
			for(int i = 1; i <= n; i ++){
				int old_i = i;
				while(A[i] <= A[i + 1] && (i <= n && i + 1 <= n)){
					i ++;
				}
				ans = max(ans, i - old_i + 1);
			}
			cout << ans << endl;
		}
	}
}

/**
2 3
47
count
switch 1 2
count
------------
2
1

3 5
747
count
switch 1 1
count
switch 1 3
count
-----------
2
3
2

*/


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

#define endl '\n'
#define pb push_back
#define fr first
#define sc second
#define ll long long int
#define bit(idx) idx&(-idx)
#define bin(x, a) bitset<a>(x)
#define all(A) A.begin(), A.end()
#define de(x) cout << #x << " = " << x << endl;

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

struct Node{
	int len4, len7, len47, len74;
	bool lazy = 0;
	Node() {}
	Node(int a, int b, int c, int d) { len4 = a, len7 = b, len47 = c, len74 = d; }
};

const int MAXN = 1e6 + 6;

int n, m;
string t;
Node segmtree[2 * MAXN];

Node f(Node a, Node b){
	return Node(a.len4 + b.len4,
	 			a.len7 + b.len7,
	 			max({(a.len4 * b.len7 != 0 ? a.len4 + b.len7 : 0), (a.len4 * b.len47 != 0 ? a.len4 + b.len47 : 0), (a.len47 * b.len7 != 0 ? a.len47 + b.len7 : 0)}),
	 			max({(a.len7 * b.len4 != 0 ? a.len7 + b.len4 : 0), (a.len7 * b.len74 != 0 ? a.len7 + b.len74 : 0), (a.len74 * b.len4 != 0 ? a.len74 + b.len4 : 0)}));
}

void build(int node, int l, int r){
	if(l == r){
		segmtree[node] = Node((t[l] == '4' ? 1 : 0), (t[l] == '7' ? 1 : 0), 0, 0);
	}
	else{
		int m = (l + r) / 2;
		build(2 * node, l, m);
		build(2 * node + 1, m + 1, r);
		segmtree[node] = f(segmtree[2 * node], segmtree[2 * node + 1]);
	}
}

void push(int node, int l, int r){
	if(segmtree[node].lazy){
		swap(segmtree[node].len4, segmtree[node].len7);
		swap(segmtree[node].len47, segmtree[node].len74);
		if(l != r){
			segmtree[2 * node].lazy ^= 1;
			segmtree[2 * node + 1].lazy ^= 1;
		}
		segmtree[node].lazy = 0;
	}
}

void upd(int node, int l, int r, int ql, int qr){
	push(node, l, r);
	if(r < ql || qr < l){
		return;
	}
	else if(ql <= l && r <= qr){
		segmtree[node].lazy ^= 1;
		push(node, l, r);
	}
	else{
		int m = (l + r) / 2;
		upd(2 * node, l, m, ql, qr);
		upd(2 * node + 1, m + 1,r , ql, qr);
		segmtree[node] = f(segmtree[2 * node], segmtree[2 * node + 1]);
	}
}

void build() { build(1, 1, n); }
void upd(int l, int r) { upd(1, 1, n, l, r); }

int main(){
	ios_base::sync_with_stdio(NULL); cin.tie(); cout.tie();
	cin >> n >> m >> t; t = ' ' + t;

	build();
	/// cout << "Legend\n4 7 47 74\n"; cout << "tree:\n"; for(int i = 1; i <= 2 * n; i ++) cout << i << ": " << segmtree[i].len4 << " " << segmtree[i].len7 << " " << segmtree[i].len47 << " " << segmtree[i].len74 << endl;

	for(int i = 0; i < m; i ++){
		string m; cin >> m;
		if(m == "count"){
			push(1, 1, n);
			cout << max({segmtree[1].len47, segmtree[1].len4, segmtree[1].len7}) << endl;
		}
		else{
			int l, r; cin >> l >> r;
			upd(l, r);
		}
	}
}
/**

*/


/**
Author: Ivan Lupov
Task: https://codeforces.com/contest/145/problem/E
Tags: segment tree, lazy
*/
#include<bits/stdc++.h>
using namespace std;

const int MAXN = 1e6;

int tree[4 * MAXN], lazy[4 * MAXN], n, m, A[MAXN];
string t;

void build(int node, int l, int r){
	if(l == r){
		tree[node] = A[l];
	}
	else{
		int m = (l + r) / 2;
		build(2 * node, l, m);
		build(2 * node + 1, m + 1, r);
		tree[node] = 0;
	}
}

void update_range(int node, int l, int r, int ql, int qr){
	if(r < ql || qr < l){
		return;
	}
	else if(ql <= l && r <= qr){
		tree[node] ^= 1;
	}
	else{
		int m = (l + r) / 2;
		update_range(2 * node, l, m, ql, qr);
		update_range(2 * node + 1, m + 1, r, ql, qr);
		tree[node] = 0;
	}
}

void push_lazy(int node, int l, int r){
	if(l == r){
		A[l] = tree[node];
	}
	else{
		tree[2 * node] ^= tree[node];
		tree[2 * node + 1] ^= tree[node];
		int m = (l + r) / 2;
		push_lazy(2 * node, l, m);
		push_lazy(2 * node + 1, m + 1, r);
	}
}

void print(){
	cout << "A = "; for(int i = 1; i <= n; i ++) cout << A[i] << " "; cout << endl;
	cout << "tree = "; for(int i = 1; i <= 2 * n; i ++) cout << tree[i] << " "; cout << endl;
}

int main(){
	/// cout << (0 ^ 1) << " " << (1 ^ 1) << " " << (0 ^ 0) << " " << (1 ^ 0) << endl;
	cin >> n >> m;
	cin >> t; t = ' ' + t;
	for(int i = 1; i <= n; i ++){
		A[i] = (t[i] == '7');
	}
	build(1, 1, n);
	for(int i = 0; i < m; i ++){
		cin >> t;
		if(t == "switch"){
			int ql, qr;
			cin >> ql >> qr;
			update_range(1, 1, n, ql, qr);
		}
		else{
			int dp[MAXN] = {0}, sz = 1;
			push_lazy(1, 1, n);
			dp[0] = A[1];
			for(int i = 2; i <= n; i ++){
				if(dp[sz - 1] <= A[i]){
					dp[sz] = A[i];
					sz ++;
				}
				else{
					for(int j = 1; j <= sz; j ++){
						if(dp[j] > A[i]){
							dp[j] = A[i];
						}
					}
				}
			}
			cout << sz << endl;
		}
	}
}

/**
2 3
47
count
switch 1 2
count
------------
2
1

3 5
747
count
switch 1 1
count
switch 1 3
count
-----------
2
3
2

*/


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

#define endl '\n'
#define pb push_back
#define fr first
#define sc second
#define ll long long int
#define bit(idx) idx&(-idx)
#define bin(x, a) bitset<a>(x)
#define all(A) A.begin(), A.end()
#define de(x) cout << #x << " = " << x << endl;

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

struct Node{
	int len4, len7, len47, len74;
	bool lazy = 0;
	Node() {}
	Node(int a, int b, int c, int d) { len4 = a, len7 = b, len47 = c, len74 = d; }
};

const int MAXN = 1e6 + 30;

int n, m;
string t;
Node segmtree[2 * MAXN + 10000];

Node f(Node a, Node b){
	return Node(a.len4 + b.len4,
	 			a.len7 + b.len7,
	 			max({(a.len4 * b.len7 != 0 ? a.len4 + b.len7 : 0), (a.len4 * b.len47 != 0 ? a.len4 + b.len47 : 0), (a.len47 * b.len7 != 0 ? a.len47 + b.len7 : 0)}),
	 			max({(a.len7 * b.len4 != 0 ? a.len7 + b.len4 : 0), (a.len7 * b.len74 != 0 ? a.len7 + b.len74 : 0), (a.len74 * b.len4 != 0 ? a.len74 + b.len4 : 0)}));
}

void build(int node, int l, int r){
	if(l == r){
		/// segmtree[node] = Node((t[l] == '4' ? 1 : 0), (t[l] == '7' ? 1 : 0), 0, 0);
		if(t[l] == '4')
			++ segmtree[node].len4;
		else
			++ segmtree[node].len7;
	}
	else{
		int m = (l + r) / 2;
		build(2 * node, l, m);
		build(2 * node + 1, m + 1, r);
		segmtree[node] = f(segmtree[2 * node], segmtree[2 * node + 1]);
	}
}

void push(int node, int l, int r){
	if(segmtree[node].lazy){
		swap(segmtree[node].len4, segmtree[node].len7);
		swap(segmtree[node].len47, segmtree[node].len74);
		if(l != r){
			segmtree[2 * node].lazy ^= 1;
			segmtree[2 * node + 1].lazy ^= 1;
		}
		segmtree[node].lazy = 0;
	}
}

void upd(int node, int l, int r, int ql, int qr){
	push(node, l, r);
	if(r < ql || qr < l){
		return;
	}
	else if(ql <= l && r <= qr){
		segmtree[node].lazy ^= 1;
		push(node, l, r);
	}
	else{
		int m = (l + r) / 2;
		upd(2 * node, l, m, ql, qr);
		upd(2 * node + 1, m + 1,r , ql, qr);
		segmtree[node] = f(segmtree[2 * node], segmtree[2 * node + 1]);
	}
}

void build() { build(1, 1, n); }
void upd(int l, int r) { upd(1, 1, n, l, r); }

int main(){
	ios_base::sync_with_stdio(NULL); cin.tie(); cout.tie();
	cin >> n >> m >> t; t = ' ' + t;

	build();
	/// cout << "Legend\n4 7 47 74\n"; cout << "tree:\n"; for(int i = 1; i <= 2 * n; i ++) cout << i << ": " << segmtree[i].len4 << " " << segmtree[i].len7 << " " << segmtree[i].len47 << " " << segmtree[i].len74 << endl;

	for(int i = 0; i < m; i ++){
		string m; cin >> m;
		if(m == "count"){
			push(1, 1, n);
			cout << max({segmtree[1].len47, segmtree[1].len4, segmtree[1].len7}) << endl;
		}
		else{
			int l, r; cin >> l >> r;
			upd(l, r);
		}
	}
}
/**

*/


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

#define endl '\n'
#define pb push_back
#define fr first
#define sc second
#define ll long long int
#define bit(idx) idx&(-idx)
#define bin(x, a) bitset<a>(x)
#define all(A) A.begin(), A.end()
#define de(x) cout << #x << " = " << x << endl;

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

struct Node{
	int len4, len7, len47, len74;
	bool lazy = 0;
	Node() {}
	Node(int a, int b, int c, int d) { len4 = a, len7 = b, len47 = c, len74 = d; }
};

const int MAXN = 1e6 + 30;

int n, m;
string t;
Node segmtree[3 * MAXN + 10000];

Node f(Node a, Node b){
	return Node(a.len4 + b.len4,
	 			a.len7 + b.len7,
	 			max({(a.len4 * b.len7 != 0 ? a.len4 + b.len7 : 0), (a.len4 * b.len47 != 0 ? a.len4 + b.len47 : 0), (a.len47 * b.len7 != 0 ? a.len47 + b.len7 : 0)}),
	 			max({(a.len7 * b.len4 != 0 ? a.len7 + b.len4 : 0), (a.len7 * b.len74 != 0 ? a.len7 + b.len74 : 0), (a.len74 * b.len4 != 0 ? a.len74 + b.len4 : 0)}));
}

void build(int node, int l, int r){
	if(l == r){
		/// segmtree[node] = Node((t[l] == '4' ? 1 : 0), (t[l] == '7' ? 1 : 0), 0, 0);
		if(t[l] == '4')
			++ segmtree[node].len4;
		else
			++ segmtree[node].len7;
	}
	else{
		int m = (l + r) / 2;
		build(2 * node, l, m);
		build(2 * node + 1, m + 1, r);
		segmtree[node] = f(segmtree[2 * node], segmtree[2 * node + 1]);
	}
}

void push(int node, int l, int r){
	if(segmtree[node].lazy){
		swap(segmtree[node].len4, segmtree[node].len7);
		swap(segmtree[node].len47, segmtree[node].len74);
		if(l != r){
			segmtree[2 * node].lazy ^= 1;
			segmtree[2 * node + 1].lazy ^= 1;
		}
		segmtree[node].lazy = 0;
	}
}

void upd(int node, int l, int r, int ql, int qr){
	push(node, l, r);
	if(r < ql || qr < l){
		return;
	}
	else if(ql <= l && r <= qr){
		segmtree[node].lazy ^= 1;
		push(node, l, r);
	}
	else{
		int m = (l + r) / 2;
		upd(2 * node, l, m, ql, qr);
		upd(2 * node + 1, m + 1,r , ql, qr);
		segmtree[node] = f(segmtree[2 * node], segmtree[2 * node + 1]);
	}
}

void build() { build(1, 1, n); }
void upd(int l, int r) { upd(1, 1, n, l, r); }

int main(){
	ios_base::sync_with_stdio(NULL); cin.tie(); cout.tie();
	cin >> n >> m >> t; t = ' ' + t;

	build();
	/// cout << "Legend\n4 7 47 74\n"; cout << "tree:\n"; for(int i = 1; i <= 2 * n; i ++) cout << i << ": " << segmtree[i].len4 << " " << segmtree[i].len7 << " " << segmtree[i].len47 << " " << segmtree[i].len74 << endl;

	for(int i = 0; i < m; i ++){
		string m; cin >> m;
		if(m == "count"){
			push(1, 1, n);
			cout << max({segmtree[1].len47, segmtree[1].len4, segmtree[1].len7}) << endl;
		}
		else{
			int l, r; cin >> l >> r;
			upd(l, r);
		}
	}
}
/**

*/


#include <stdio.h>
#include <algorithm>

#define N 1000100
#define left(i) (i<<1)
#define right(i) (left(i)+1)
typedef struct {
  int a1;
  int a2;
  int a3;
  int a4;
}node;
node seg[N*4];
bool lz[N*4];
char arr[N];

void prop(int id, int l, int r){
  if(lz[id]){
	if(l != r-1)lz[left(id)] = lz[right(id)] = lz[id];
	std::swap(seg[id].a1,seg[id].a2);
	std::swap(seg[id].a3,seg[id].a4);
  };
  lz[id] = 0;
};

void make(int id, int l, int r){
  if(l>=r)return;
  if(l == r-1){
	if(arr[l] == '4'){
	  seg[id].a1=1;
	  seg[id].a3=1;
	  seg[id].a4=1;
	}else{
	  seg[id].a2=1;
	  seg[id].a3=1;
	  seg[id].a4=1;
	};
  }else{
	int m=(l+r)>>1;
	make(left(id),l,m);
	make(right(id),m,r);
	seg[id].a1 = seg[left(id)].a1+seg[right(id)].a1;
	seg[id].a2 = seg[left(id)].a2+seg[right(id)].a2;
	seg[id].a3 = std::max(seg[left(id)].a1+seg[right(id)].a3,seg[left(id)].a3+seg[right(id)].a2);
	seg[id].a4 = std::max(seg[left(id)].a2+seg[right(id)].a4,seg[left(id)].a4+seg[right(id)].a1);
  };
};
void upd(int id, int l, int r,int a, int b){
  if(l >= r)return;
  prop(id,l,r);
  if(a >= r || l >= b)return;
  if(a<=l&&r<=b){
	lz[id] = 1;
	prop(id,l,r);
  }else{
	int m=(l+r)>>1;
	upd(left(id),l,m,a,b);
	upd(right(id),m,r,a,b);
	seg[id].a1 = seg[left(id)].a1+seg[right(id)].a1;
	seg[id].a2 = seg[left(id)].a2+seg[right(id)].a2;
	seg[id].a3 = std::max(seg[left(id)].a1+seg[right(id)].a3,seg[left(id)].a3+seg[right(id)].a2);
	seg[id].a4 = std::max(seg[left(id)].a2+seg[right(id)].a4,seg[left(id)].a4+seg[right(id)].a1);
  };
};
char qry[100];
int main(void){
  int n,m;
  scanf("%d %d",&n,&m);
  scanf("%s",arr);
  make(1,0,n);
  for(int i=0;i<m;i++){
	scanf("%s",qry);
	if(qry[0] == 'c'){
	  printf("%d\n",seg[1].a3);
	}else{
	  int l,r;
	  scanf("%d %d",&l,&r);
	  upd(1,0,n,l-1,r);
	};
  };
  return 0;
};


#include <stdio.h>
#include <algorithm>

#define N 1000100
#define left(i) (i<<1)
#define right(i) (left(i)+1)
typedef struct {
  int a1;
  int a2;
  int a3;
  int a4;
}node;
node seg[N*4];
bool lz[N*4];
char arr[N];

void prop(int id, int l, int r){
  if(l != r-1)lz[left(id)] = lz[right(id)] = lz[id];
  if(lz[id]){
	std::swap(seg[id].a1,seg[id].a2);
	std::swap(seg[id].a3,seg[id].a4);
  };
  lz[id] = 0;
};

void make(int id, int l, int r){
  if(l>=r)return;
  if(l == r-1){
	if(arr[l] == '4'){
	  seg[id].a1=1;
	  seg[id].a3=1;
	  seg[id].a4=1;
	}else{
	  seg[id].a2=1;
	  seg[id].a3=1;
	  seg[id].a4=1;
	};
  }else{
	int m=(l+r)>>1;
	make(left(id),l,m);
	make(right(id),m,r);
	seg[id].a1 = seg[left(id)].a1+seg[right(id)].a1;
	seg[id].a2 = seg[left(id)].a2+seg[right(id)].a2;
	seg[id].a3 = std::max(seg[left(id)].a1+seg[right(id)].a3,seg[left(id)].a3+seg[right(id)].a2);
	seg[id].a4 = std::max(seg[left(id)].a2+seg[right(id)].a4,seg[left(id)].a4+seg[right(id)].a1);
  };
};
void upd(int id, int l, int r,int a, int b){
  if(l >= r)return;
  prop(id,l,r);
  if(a >= r || l >= b)return;
  if(a<=l&&r<=b){
	lz[id] = 1;
	prop(id,l,r);
  }else{
	int m=(l+r)>>1;
	upd(left(id),l,m,a,b);
	upd(right(id),m,r,a,b);
	seg[id].a1 = seg[left(id)].a1+seg[right(id)].a1;
	seg[id].a2 = seg[left(id)].a2+seg[right(id)].a2;
	seg[id].a3 = std::max(seg[left(id)].a1+seg[right(id)].a3,seg[left(id)].a3+seg[right(id)].a2);
	seg[id].a4 = std::max(seg[left(id)].a2+seg[right(id)].a4,seg[left(id)].a4+seg[right(id)].a1);
  };
};
char qry[100];
int main(void){
  int n,m;
  scanf("%d %d",&n,&m);
  scanf("%s",arr);
  make(1,0,n);
  for(int i=0;i<m;i++){
	scanf("%s",qry);
	if(qry[0] == 'c'){
	  printf("%d\n",seg[1].a3);
	}else{
	  int l,r;
	  scanf("%d %d",&l,&r);
	  upd(1,0,n,l-1,r);
	};
  };
  return 0;
};


#include <stdio.h>
#include <algorithm>

#define N 1000100
#define left(i) (i<<1)
#define right(i) (left(i)+1)
typedef struct {
  int a1;
  int a2;
  int a3;
  int a4;
}node;
node seg[N*4];
int lz[N*4];
char arr[N];

void prop(int id, int l, int r){
  if(lz[id]%2){
	if(l != r-1){
	  lz[left(id)] += lz[id];
	  lz[right(id)] += lz[id];
	};
	std::swap(seg[id].a1,seg[id].a2);
	std::swap(seg[id].a3,seg[id].a4);
	lz[id] = 0;
  };
};

void make(int id, int l, int r){
  if(l>=r)return;
  if(l == r-1){
	if(arr[l] == '4'){
	  seg[id].a1=1;
	  seg[id].a3=1;
	  seg[id].a4=1;
	}else{
	  seg[id].a2=1;
	  seg[id].a3=1;
	  seg[id].a4=1;
	};
  }else{
	int m=(l+r)>>1;
	make(left(id),l,m);
	make(right(id),m,r);
	seg[id].a1 = seg[left(id)].a1+seg[right(id)].a1;
	seg[id].a2 = seg[left(id)].a2+seg[right(id)].a2;
	seg[id].a3 = std::max(seg[left(id)].a1+seg[right(id)].a3,seg[left(id)].a3+seg[right(id)].a2);
	seg[id].a4 = std::max(seg[left(id)].a2+seg[right(id)].a4,seg[left(id)].a4+seg[right(id)].a1);
  };
};
void upd(int id, int l, int r,int a, int b){
  if(l >= r)return;
  prop(id,l,r);
  if(a >= r || l >= b)return;
  if(a<=l&&r<=b){
	lz[id] += 1;
	prop(id,l,r);
  }else{
	int m=(l+r)>>1;
	upd(left(id),l,m,a,b);
	upd(right(id),m,r,a,b);
	seg[id].a1 = seg[left(id)].a1+seg[right(id)].a1;
	seg[id].a2 = seg[left(id)].a2+seg[right(id)].a2;
	seg[id].a3 = std::max(seg[left(id)].a1+seg[right(id)].a3,seg[left(id)].a3+seg[right(id)].a2);
	seg[id].a4 = std::max(seg[left(id)].a2+seg[right(id)].a4,seg[left(id)].a4+seg[right(id)].a1);
  };
};
char qry[100];
int main(void){
  int n,m;
  scanf("%d %d",&n,&m);
  scanf("%s",arr);
  make(1,0,n);
  for(int i=0;i<m;i++){
	scanf("%s",qry);
	if(qry[0] == 'c'){
	  printf("%d\n",seg[1].a3);
	}else{
	  int l,r;
	  scanf("%d %d",&l,&r);
	  upd(1,0,n,l-1,r);
	};
  };
  return 0;
};


#include <stdio.h>
#include <algorithm>

#define N 1000100
#define left(i) (i<<1)
#define right(i) (left(i)+1)
typedef struct {
  int a1;
  int a2;
  int a3;
  int a4;
}node;
node seg[N*4];
int lz[N*4];
char arr[N];

void prop(int id, int l, int r){
  if(lz[id]%2){
	if(l != r-1){
	  lz[left(id)] += lz[id];
	  lz[right(id)] += lz[id];
	};
	std::swap(seg[id].a1,seg[id].a2);
	std::swap(seg[id].a3,seg[id].a4);
	lz[id] = 0;
  };
};

void make(int id, int l, int r){
  if(l>=r)return;
  if(l == r-1){
	if(arr[l] == '4'){
	  seg[id].a1=1;
	  seg[id].a3=1;
	  seg[id].a4=1;
	}else{
	  seg[id].a2=1;
	  seg[id].a3=1;
	  seg[id].a4=1;
	};
  }else{
	int m=(l+r)>>1;
	make(left(id),l,m);
	make(right(id),m,r);
	seg[id].a1 = seg[left(id)].a1+seg[right(id)].a1;
	seg[id].a2 = seg[left(id)].a2+seg[right(id)].a2;
	seg[id].a3 = std::max(seg[left(id)].a1+seg[right(id)].a3,seg[left(id)].a3+seg[right(id)].a2);
	seg[id].a4 = std::max(seg[left(id)].a2+seg[right(id)].a4,seg[left(id)].a4+seg[right(id)].a1);
  };
};
void upd(int id, int l, int r,int a, int b){
  if(l >= r)return;
  prop(id,l,r);
  if(a >= r || l >= b)return;
  if(a<=l&&r<=b){
	lz[id] += 1;
	prop(id,l,r);
  }else{
	int m=(l+r)>>1;
	upd(left(id),l,m,a,b);
	upd(right(id),m,r,a,b);
	seg[id].a1 = seg[left(id)].a1+seg[right(id)].a1;
	seg[id].a2 = seg[left(id)].a2+seg[right(id)].a2;
	seg[id].a3 = std::max(seg[left(id)].a1+seg[right(id)].a3,seg[left(id)].a3+seg[right(id)].a2);
	seg[id].a4 = std::max(seg[left(id)].a2+seg[right(id)].a4,seg[left(id)].a4+seg[right(id)].a1);
  };
};
char qry[100];
int main(void){
  int n,m;
  scanf("%d %d",&n,&m);
  scanf("%s",arr);
  make(1,0,n);
  for(int i=0;i<m;i++){
	scanf("%s",qry);
	if(qry[0] == 'c'){
	  printf("%d\n",seg[1].a3);
	}else{
	  int l,r;
	  scanf("%d %d",&l,&r);
	  upd(1,0,n,l-1,r);
	  for(int i = l-1;i<r;i++){
		if(arr[i]=='4')arr[i]='7';
		else arr[i]='4';
	  };
	};
  };
  return 0;
};


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

typedef int ll;
#define MAXN 300005

struct node
{

	int no_4;
	int no_7;
	int no_47;
	int no_74;
	int prop;

	node(int a=0, int b=0, int c=0, int d=0, int e=0) : no_47(c), no_4(a), no_74(d), no_7(b), prop(e) {}
};

ll n;
node tree[4*MAXN];

#define LEFT(x) (x*2)
#define RIGHT(x) (x*2+1)
#define MAX(x, y) (max(x,y))
#define SUM(x,y) (x+y)
#define XOR(x,y) (x^y)
#define OR(x,y) (x|y)

#define FUNC func
#define ASSOCIATIVE_CONSTANT 0

/*
inline void fastIO(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}
*/
inline int max(int a, int b, int c){
	return max(a, max(b, c));
}

inline node func(node a, node b)
{
	return node(a.no_4+b.no_4, a.no_7+b.no_7, max(a.no_47+b.no_7, a.no_4+b.no_47, a.no_4+b.no_7),
			max(a.no_74+b.no_4, a.no_7+b.no_74, a.no_7+b.no_4));
}

string hell;

void build(int root, int tree_left, int tree_right)
{
	if(tree_left == tree_right){
		if(hell[tree_left] == '4')
			tree[root] = node(1, 0, 1, 1);
		else
			tree[root] = node(0, 1, 1, 1);
	}
	else{
		ll tree_mid = (tree_left + tree_right)/2;
		build(LEFT(root), tree_left, tree_mid); /// Building the left tree
		build(RIGHT(root), tree_mid+1, tree_right); /// Building the right tree
		tree[root] = func(tree[LEFT(root)], tree[RIGHT(root)]); /// Preprocessing the data
	}
}
/*
ll query(ll root, ll tree_left, ll tree_right, ll query_left, ll query_right)
{
	// cout << root << " " << tree_left << " " << tree_right << " " << query_left << " " << query_right << "\n";
	if(query_left >  query_right)
		return ASSOCIATIVE_CONSTANT; /// 0 for sum, INT_MAX for min, INT_MIN for max, ...
	if(tree_left == query_left && tree_right == query_right)
		return tree[root]; /// Bottom of the tree
	ll tree_mid = (tree_left + tree_right)/2;
	return FUNC(query(LEFT(root), tree_left, tree_mid, query_left, min(query_right, tree_mid)),
			query(RIGHT(root), tree_mid+1, tree_right, max(query_left, tree_mid+1), query_right));
}

void update(ll root, ll tree_left, ll tree_right, ll pos, ll val)
{
	if(tree_left == tree_right)
		tree[root] = val; /// Update the bottom
	else{
		ll tree_mid = (tree_left + tree_right)/2;
		if(pos>tree_mid) /// In the right sub-tree
			update(RIGHT(root), tree_mid+1, tree_right, pos, val);
		else /// In the left sub-tree
			update(LEFT(root), tree_left, tree_mid, pos, val);
		/// Updating
		tree[root] = FUNC(tree[LEFT(root)], tree[RIGHT(root)]);
	}
}
*/
void updateRange(ll root, ll tree_left, ll tree_right, ll up_left, ll up_right, ll val)
{
	if(tree[root].prop){
		/// This node needs to be updated

		swap(tree[root].no_47, tree[root].no_74);
		swap(tree[root].no_4, tree[root].no_7);
		tree[root] = 0;
	}

	if(tree_left > up_right || tree_left > tree_right || tree_right < up_left)
		return;
	else if(up_left <= tree_left && tree_right <= up_right){
		//printf("AKA: %I64d %I64d %I64d %I64d %I64d %I64d\n", root, tree_left, tree_right, up_left, up_right, val);
		swap(tree[root].no_47, tree[root].no_74);
		swap(tree[root].no_4, tree[root].no_7);
		//printf("TREE{ROOT} : %I64d\n", tree[root]);
		if(tree_left != tree_right){
			tree[LEFT(root)].prop ^= 1;
			tree[RIGHT(root)].prop ^= 1;
		}
	}
	else{
		ll tree_mid = (tree_left + tree_right)/2;
		updateRange(LEFT(root), tree_left, tree_mid, up_left, up_right, val);
		updateRange(RIGHT(root), tree_mid+1, tree_right, up_left, up_right, val);

		tree[root] = FUNC(tree[LEFT((root))], tree[RIGHT(root)]);
	}
	return;
}

void print_segment_tree(int lim = 40)
{
	printf("\nSegment tree:\n");
	for(int i=1, j=1; i<lim; ++i){
		if(i == (1<<j)){
			++j;
			printf("\n");
		}
		printf("(%d,%d,%d,%d) ", tree[i].no_4, tree[i].no_7, tree[i].no_47, tree[i].no_74);
	}
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	int n, m, p, q;
	cin >> n >> m;

	string x;
	cin >> hell;

	build(1, 0, n-1);
	while(m--){
		cin >> x;
		if(x[0] == 'c'){
			cout << tree[1].no_47 << endl;
			//print_segment_tree(7);
		}
		else{
			cin >> p >> q;
			updateRange(1, 0, n-1, --p, --q, p);
			//print_segment_tree(7);
		}
	}
	return 0;
}