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

int main(void) {
    long long n;
    cin >> n;
    char digits[19];
    sprintf(digits, "%lld", n);

    int len = strlen(digits);

    int lim = 1 << len;
    int ans = INT_MAX;
    for (int i = 1; i <= lim; ++i) {
        int cur = 0, cnt = len;
        for (int j = 0; j < len; ++j) {
            if ((i >> j) & 1) {
                cur += digits[j] - '0';
                cnt--;
            }
        }
        if (cur % 3 == 0) ans = min(ans, cnt);
    }
    if (ans == len)
        cout << -1 << endl;
    else
        cout << ans << endl;
    return 0;
}
#include<bits/stdc++.h>
#include <iostream>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define repd(i, n) for(int i = 0; i <= (n); ++i)
using ll = long long;
using Graph = vector<vector<int>>;
using P = pair<int, int>;
using Pll = pair<long long, long long>;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }

int main() {
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i, n) cin >> a[i];
    map<int, int> mp;
    ll ans = 0;
    rep(j, n) {
        ans += j-mp[a[j]];
        mp[a[j]]++;
    }
    cout << ans << endl;
    return 0;
}
