#include <bits/stdc++.h>
#include <iostream>
#include <string>

using namespace std;
#define rep(i,n) for(long long i=0; i<(long long)(n);i++)
//rep…「0からn-1まで」の繰り返し
#define rep2(i,s,n) for(long long i=s; i<=(long long)(n);i++)
//rep2…「sからnまで」の繰り返し
#define repr(i,s,n) for(long long i=s;i>=(long long)(n);i--)
//repr…「ｓからnまで」の降順の繰り返し

typedef long long ll;

const ll inf = 1e18+7;
const ll mod = 1e9+7;

int main(){
    string n;
    cin>>n;

    int cnt1=0,cnt2=0,sum=0;

    rep(i,n.size()){
        int m=n[i]-'0';
        sum+=m;

        if(m%3==1)cnt1++;
        else if(m%3==2)cnt2++;
    }

    int ans=-1;

    if(sum%3==0) ans=0;

    else if(sum%3==1){
        if(cnt1>=1&&n.size()>1)ans=1;
        else if(cnt2>=2&&n.size()>2)ans=2;
    }

    else {
        if(cnt2>=1&&n.size()>1)ans=1;
        else if(cnt1>=2&&n.size()>2)ans=2;
    }

    cout<<ans<<endl;
}

#include<iostream>
#include<vector>
#include<algorithm>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<string.h>
#include<math.h>
#define ll long long
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define pb push_back
#define F first
#define S second
#define MAX 500005
#define MOD (ll)(1e9+7)
#define INF (ll)(1e18)
#define AC ios::sync_with_stdio(0);cin.tie(0);
using namespace std;
signed main(){
    AC;
    int n;
    ll a[200005],pre[200005]={0},maxn=0;
    cin>>n;
    FOR(i,1,n+1){
        cin>>a[i];
        pre[i]=pre[i-1]+a[i];
    }
    ll now=0,ans=0;
    FOR(i,1,n+1){
        ans=max(ans,now+maxn);
        maxn=max(maxn,pre[i]);
        now+=pre[i];
    }
    ans=max(ans,now);
    cout<<ans<<endl;
}
