#include<bits/stdc++.h>
#define maxn 250000
using namespace std;
int n;
char s[maxn];
int main()
{
	scanf("%d",&n);
	scanf("%s",s+1);
	if(s[1]!=s[n])
		cout<<1<<endl;
	else
	{
		int ok=1;
		for(int i=1;i<n;i++)
			if(s[i]!=s[1]&&s[i+1]!=s[n])
				ok=0;
		if(ok==0)
			cout<<2<<endl;
		else
			cout<<-1<<endl;
	}
	
	return 0;
}
#include <iostream>
#include <vector>
#include <utility>
#include <string>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <numeric>
#include <map>

#define fore(i, l, r) for(int i = int(l); i < int(r); i++) //Python style baby
#define sz(a) int((a).size())
#define mp make_pair
#define vi vector<int>
#define vpi vector<pair<int, int>>
#define vl vector<long long>
#define vpl vector<pair<long long, long long>>

using namespace std;
using ll = long long;
int mode, t;


void solve() {
  string s;
  cin >> s;
  reverse(s.begin(), s.end());

  map<char, char> d;
  d['6'] = '9';
  d['9'] = '6';
  d['0'] = '0';
  d['8'] = '8';
  d['1'] = '1';
  for (char c : s) {
    cout << d[c];
  }
  cout << endl;
}



int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  mode = 0;
  if (mode == 0) { 
    solve();
  } else {
    cin >> t;
    while (t--) {
      solve();
    }
  }
}
