#include <iostream>

using namespace std;

int main()
{
 int a,b,sumA=0,sumB=0;
 cin>>a>>b;
 while(a!=0)
 {

   sumA+= a%10;
   a/=10;
   sumB+= b%10;
   b/=10;
 }
 if(sumA>=sumB)
   cout<<sumA;
else
    cout<<sumB;

}


#include <bits/stdc++.h>
#include <cctype>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//template<typename key, typename val>
//using ordered_tree =
//  tree<key, val, std::less<>, rb_tree_tag, tree_order_statistics_node_update>;
using namespace std;
typedef long long int64;
typedef unsigned long long uint64;
typedef complex<double> comp;
const double pi = 3.14159265358979323846;
const int inf = (int)1e+9 + 2;
const int64 inf64 = (int64)1e+18 + 2;
const double dinf = 1e+20;
const int mod = /*1'000'000'007;//*/998244353;
const int base = 2187;
const double eps = 1e-9;
const int N = 500'000;
const int LOGN = 19;

int n, m, k;

void solve(int test) {
  int64 n;
  cin >> n;
  int a[10] = {};
  int s = 0, t = 0;
  for (; n; n /= 10, ++t) {
    ++a[n % 10 % 3];
    s += n % 10;
  }
  if (s % 3 == 0) {
    cout << "0\n";
    return;
  }
  if (s % 3 == 1) {
    if (a[1] && t > 1) {
      cout << "1\n";
      return;
    }
    if (a[2] >= 2 && t > 2) {
      cout << "2\n";
      return;
    }
    cout << "-1\n";
    return;
  }
  if (a[2] && t > 1) {
    cout << "1\n";
    return;
  }
  if (a[1] >= 2 && t > 2) {
    cout << "2\n";
    return;
  }
  cout << "-1\n";
}

void precalc() {
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  precalc();
  int test = 1;
  //cin >> test;
  //auto start = chrono::high_resolution_clock::now();
  for (int i = 1; i <= test; ++i) {
    solve(i);
  }
  //cerr << chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - start).count() << '\n';
  return 0;
}

