#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rrep(i, n) for(int i = 1; i <= (int)(n); i++)
#define drep(i, n) for(int i = (n)-1; i >= 0; i--)
#define ALL(x) (x).begin(), (x).end()
#define dup(x,y) (((x)+(y)-1)/(y))
#define srep(i,s,t) for (int i = s; i < t; ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const int mod = 1000000007;
// const int mod = 998244353;

string s;
int n;
int ans = INF;

bool is3(string t) {
  int now = 0;
  rep(i, t.size()) now += t[i] - '0';
  return now%3 == 0;
}

void func(string t, int i) {
  if (i == n) {
    if (is3(t)) ans = min(ans, n - (int)t.size());
    return;
  }
  func(t+s[i], i+1);
  func(t, i+1);
}

int main() {
  cin >> s;
  n = s.size();
  string t = "";
  func(t, 0);
  if (ans >= n) ans =-1;
  cout << ans << endl;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll,ll>
#define FOR(I,A,B) for(ll I = ll(A); I < ll(B); ++I)
#define FORR(I,A,B) for(ll I = ll((B)-1); I >= ll(A); --I)
#define TO(x,t,f) ((x)?(t):(f))
#define SORT(x) (sort(x.begin(),x.end())) // 0 2 2 3 4 5 8 9
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) //xi>=v  x is sorted
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) //xi>v  x is sorted
#define NUM(x,v) (POSU(x,v)-POSL(x,v))  //x is sorted
#define REV(x) (reverse(x.begin(),x.end())) //reverse
ll gcd_(ll a,ll b){if(a%b==0)return b;return gcd_(b,a%b);}
ll lcm_(ll a,ll b){ll c=gcd_(a,b);return ((a/c)*b);}
#define NEXTP(x) next_permutation(x.begin(),x.end())
const ll INF=ll(1e16)+ll(7);
const ll MOD=1000000007LL;
#define out(a) cout<<fixed<<setprecision((a))
//tie(a,b,c) = make_tuple(10,9,87);
#define pop_(a) __builtin_popcount((a))
ll keta(ll a){ll r=0;while(a){a/=10;r++;}return r;}


int main(){

	ll N;
	cin >> N;
	vector<ll> X(N),Y(N),Z(N);
	FOR(i,0,N) cin >> X[i] >> Y[i] >> Z[i];

	ll dp[1LL<<N][N];
	FOR(i,0,(1<<N))FOR(j,0,N){
		dp[i][j] = INF;
	}
	dp[0][0] = 0;

	FOR(s,0,(1<<N)){
		FOR(j,0,N){//now
			FOR(k,0,N){//to
				ll c = abs(X[j]-X[k])+abs(Y[j]-Y[k])+max(0LL,Z[k]-Z[j]);
				dp[s|(1<<k)][k] = min( dp[s|(1<<k)][k] , dp[s][j]+c );
			}
		}
	}

	cout << dp[(1<<N)-1][0] << endl;


}




