/*#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
*/

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

#define ll long long
#define f first
#define s second
#define Fast ios_base::sync_with_stdio(false);cin.tie(NULL);
typedef pair<ll , pair<ll, ll> > pi;
int pow(int x,int y){
    int res=1;
    while(y){
        if(y&1) res*=x;
        y>>=1;
        x*=x;
    }
    return res;
}   
struct Compare {
    constexpr bool operator()(pi const & a,
                              pi const & b) const noexcept
    { return a.first < b.first || (a.first == b.first && a.second.first > b.second.first); }
 
};
void prefix_function( string s,ll arr[] )
{
     long long border=0; arr[0]=0;
    for(long long i=1;i<s.size();i++)
    {
      while(border>0 && s[i]!=s[border])
        border=arr[border-1];
     
     if(s[i]==s[border])
         border++;
      else
       border=0;
      
      arr[i]=border;
    }
}//send mod-2 for a^-1 if mod is a prime number

ll mod=998244353;
ll add( ll a , ll b)
{
    return (((a%mod)+(b%mod))%mod);
}
ll mul(ll a,ll b)
{
  return (((a%mod)*(b%mod))%mod);
}
ll binpow(ll a, ll b) {
    ll res = 1;
    while (b) {
        if (b & 1) res = mul(res, a);
        a = mul(a, a);
        b >>= 1;
    }
    return res;
}
ll subs(ll a,ll b)
{
    return (((a%mod)-(b%mod)+mod)%mod);
}
ll dv(ll a,ll b)
{
    ll inv=binpow(b,mod-2);
    return mul(a,inv);
}

ll dsu_arr[100000];
ll dsu_sz[100000];
void dsu(ll n)
{
    for(ll i=0;i<=n;i++)
    {
        dsu_arr[i]=i;
        dsu_sz[i]=1;
    }
}
ll find(ll x)
{
     ll root=x;
    while (root!=dsu_arr[root])
    {
        root=dsu_arr[root];
    }
    while(x!=dsu_arr[x])
    {
        dsu_arr[x]=root;
        x=dsu_arr[x];
    }
    return root;
}
ll merge(ll x,ll y)
{
    ll root1=find(x);
    ll root2=find(y);
    
    if(root1==root2)
    return 0ll;
 
    if(dsu_sz[x]>dsu_sz[y]){
    dsu_arr[root2]=root1;
    dsu_sz[root1]+=dsu_sz[root2];
    }
    else
    {
         dsu_sz[root2]+=dsu_sz[root1];
        dsu_arr[root1]=root2;
    }
    return 1ll;
}
/*
vector<ll>adj[100005];
bool vis[100005];
ll dist[100005];
void bfs(ll c)
{
  vis[c]=true;
  dist[c]=0;
  queue<ll>q;
  q.push(c);
while(!q.empty())
{
    ll x=q.front();
    q.pop();
   
    for(ll i=0;i<adj[x].size();i++)
    {
        ll y=adj[x][i];
        if(!vis[y])
        {
            vis[y]=true;
            dist[y]=dist[x]+1;
            
            q.push(y);
        }
    }
 
}
}
*/

int main()
{
    Fast
ll n;
     string s;
     cin>>s;
      n=s.size();
     vector<ll>sura;
    ll mx=-1;
  for(ll mask=0;mask<(1<<n);mask++)
  {
     ll sum=0;
     ll h=0;
      for(ll i=0;i<n;i++ )
      {
          if(mask& (1<<i))
          {
              h++;
              sum+=(s[i]-'0');
          }
      }
      if(sum%3==0 && sum>0)
      {
          mx=max(mx,h);
      }
  }
  if(mx==-1)
  {
      cout<<mx<<endl;
  }
  else
  {
      cout<<n-mx<<endl;
  }
  



}



   
      
    
        


#include <algorithm>
#include <vector>
#include <cstdio>
#include <cmath>
#define int long long
const int N = 100005, INF = 0x3f3f3f3f3f3f3f3fll;
int n, x, ans = INF, cnt, tot;
std::vector<int> R, G, B, a[3], b[2];
char c[5];
int find(std::vector<int> a, std::vector<int> b) {
    int an = INF;
    if(a.size() == 0 || b.size() == 0) return an;
    for(int i = 0; i < (signed)a.size(); i++) {
        int pos = std::lower_bound(b.begin(), b.end(), a[i]) - b.begin();
        int x = INF;
        if(pos == 0) x = std::abs(b[0] - a[i]);
        else if(pos == b.end() - b.begin()) x = std::abs(b[pos-1]-a[i]);
        else x = std::min(std::abs(b[pos]-a[i]), std::abs(b[pos-1]-a[i]));
        an = std::min(an, x); 
    }
    return an;
}
signed main() {
    scanf("%lld", &n);
    n = 2 * n;
    for(int i = 1; i <= n; i++) {
        scanf("%lld%s", &x, c);
        if(c[0] == 'R') R.push_back(x);
        if(c[0] == 'G') G.push_back(x);
        if(c[0] == 'B') B.push_back(x);
    }
    // printf("%d %d %d", R.size(), G.size(), B.size());
    if(R.size() % 2 == 0 && G.size() % 2 == 0 && B.size() % 2 == 0)
        return puts("0"), 0;
    if(R.size() % 2 == 1) a[++cnt] = R; else b[++tot] = R; 
    if(G.size() % 2 == 1) a[++cnt] = G; else b[++tot] = G;
    if(B.size() % 2 == 1) a[++cnt] = B; else b[++tot] = B;
    std::sort(a[1].begin(), a[1].end());
    std::sort(a[2].begin(), a[2].end());
    std::sort(b[1].begin(), b[1].end());
    ans = find(a[1], a[2]);
    ans = std::min(find(a[1], b[1]) + find(a[2], b[1]), ans);
    printf("%lld", ans);
    return 0;
}