#include <bits/stdc++.h>
using namespace std;
#define ll long long 
char s[200010];
char d1[]={'1', '1', '0'};
char d2[]={'1', '0', '1'};
char d3[]={'0', '1', '1'};

int main()
{
	ll i, j, n, m;

	scanf("%lld %s", &n, s);
	if(n==1)
	{
		if(s[0]=='1') printf("20000000000\n");
		else printf("10000000000\n");
		return 0;
	}
	ll ex=0;
	ll in=0;
	for(i=0; i<n; i++) if(s[i]!=d1[i%3]) in=1;

	if(in==0)
	{
		n=(n+2)/3;
		n=1e10-n +1;
		printf("%lld\n", n);
		return 0;
	}
	in=0;
	for(i=0; i<n; i++) 
		{
			if(s[i]!=d2[i%3]) in=1;
			//cout<<s[i]<<" "<<d2[i%3]<<endl;
		}
	if(in==0)
	{
		n+=1;
		n=(n+2)/3;
		n=1e10-n +1;
		printf("%lld\n", n);
		return 0;
	}
	in=0;
	for(i=0; i<n; i++) if(s[i]!=d3[i%3]) in=1;
	if(in==0)
	{
		n+=2;
		n=(n+2)/3;
		n=1e10-n +1;

		printf("%lld\n", n);
		return 0;
	}

	printf("0\n");






	

	return 0;
}

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)

using namespace std;
using LL = long long;
using P = pair<int,int>;
using vv = vector<vector<int>>;
const int INF = (int)1e9;
const LL LINF = (LL)1e18;
const int query = 1000;
const int grid_size = 30;
const int grid_total = grid_size * grid_size;
const int initial_length = 5000;

string path_ex(int si, int sj, int ti, int tj){
	int di = ti - si, dj = tj - sj;
	string s;
	if(di >= 0) rep(k,di) s.push_back('D');
	else rep(k,-di) s.push_back('U');
	if(dj >= 0) rep(k,dj) s.push_back('R');
	else rep(k,-dj) s.push_back('L');
	return s;
}

vector<vector<int>> G;
int cost[grid_total][grid_total];

string dijkstra(int st, int ed){
   int N = G.size();
   vector<int> dist(N, INF), par(N, -1);
   dist[st] = 0;
   priority_queue<P,vector<P>,greater<P>> q;
   q.push({dist[st], st});
   while(!q.empty()){
	   P p = q.top(); q.pop();
	   int d = p.first, n = p.second;
	   if(dist[n] < d) continue;
	   for(auto v : G[n]){
		   int c = cost[n][v];
		   if(dist[v] <= d + c) continue;
		   dist[v] = d + c;
		   q.push({dist[v], v});
		   par[v] = n;
	   }
   }
   string path;
   int now = ed;
   while(now != st){
	   char c;
	   if(par[now] + 1 == now) c = 'R';
	   else if(par[now] - 1 == now) c = 'L';
	   else if(par[now] + grid_size == now) c = 'D';
	   else c = 'U';
	   path.push_back(c);
	   now = par[now];
   }
   reverse(path.begin(), path.end());
   return path;
}

int main(){
	//FILE *outputfile;
	//outputfile = freopen("test.txt", "w", stdout);

	G.resize(grid_total);
	rep(i,grid_size){
		rep(j,grid_size) cost[i][j] = INF;
	}
	rep(i,grid_size){
		rep(j,grid_size-1){
			int l = i * grid_size + j, r = i * grid_size + j + 1;
			assert(l < grid_total and r < grid_total);
			G[l].emplace_back(r);
			G[r].emplace_back(l);
			cost[l][r] = initial_length, cost[r][l] = initial_length;
		}
	}
	rep(j,grid_size){
		rep(i,grid_size-1){
			int u = i * grid_size + j, d = (i + 1) * grid_size + j;
			assert(u < grid_total and d < grid_total);
			G[u].emplace_back(d);
			G[d].emplace_back(u);
			cost[u][d] = initial_length, cost[d][u] = initial_length;
		}
	}
	rep(q,query){
		int si, sj, ti, tj;
		cin >> si >> sj >> ti >> tj;
		int st = si * grid_size + sj, ed = ti * grid_size + tj;
		assert(st < grid_total and ed < grid_total);
		string path = dijkstra(st, ed);
		cout << path << endl;
		int dist_result;
		cin >> dist_result;
		int now = st, pre = -1;
		int path_size = path.size();
		rep(k,path_size){
			pre = now;
			if(path[k] == 'R') now++;
			else if(path[k] == 'L') now--;
			else if(path[k] == 'D') now += grid_size;
			else now -= grid_size;
			int now_length = cost[pre][now];
			int ave_length = dist_result / path_size;
			cost[pre][now] = (now_length * (query - q - 1) + ave_length * (q + 1)) / query;
		}
	}

	return 0;
}
