#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <cstdint>
#include <numeric>
#define INF 1000000000
#define LLINF 2000000000000000
#define MOD 1000000007
using namespace std;
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define LOOP(i,N) for(int i=0;i<N;i++)
#define LOOP1(i,N) for(int i=1;i<=N;i++)
typedef pair<int,int> P;
typedef pair<int,pair<int,int> > PP;
// #define int long long

signed main(){
  string s;
  cin >> s;
  int a[s.length()];
  int n = s.length();
  int sum = 0;
  int cnt[3] = {};
  LOOP(i,s.length()){
    a[i] = (s[i] - '0')%3;
    sum += a[i];
    cnt[a[i]%3]++;
  }
  int ans = -1;
  if(sum%3 == 0){
    ans = 0;
  }else{
    if(cnt[sum%3]){
      ans = 1;
    }else{
      int c = sum%3 == 2 ? 1 : 2;
      if(cnt[c] >= 2){
        ans = 2;
      }else{
        cerr << c << ":" << cnt[c] << endl;
        cout << -1 << endl;
        return 0;
      }
    }
  }

  if(n <= ans) {
    cout << -1 << endl;
  }else{
    cout << ans << endl;
  }
}

#include<bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &x){
	x=0;char ch=getchar();bool f=false;
	while(!isdigit(ch)){if(ch=='-'){f=true;}ch=getchar();}
	while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
	x=f?-x:x;
	return ;
}
template <typename T>
inline void write(T x){
	if(x<0) putchar('-'),x=-x;
	if(x>9) write(x/10);
	putchar(x%10^48);
	return ;
}
#define ll long long
const int N=2e5+5,M=1e8;
int n,m,q;
ll a[N],b[N];
struct SGTree{
	int cur,root;
	ll sum[N*80];
	int num[N*80],ls[N*80],rs[N*80];
	void Modify(int &x,int l,int r,int pos,int v,int tp){
		if(!x) x=++cur;
		int mid=l+r>>1;sum[x]+=v*tp;num[x]+=tp;
		if(l==r) return ;
		if(pos<=mid) Modify(ls[x],l,mid,pos,v,tp);
		else Modify(rs[x],mid+1,r,pos,v,tp);
		return ;
	}
	ll QuerySum(int x,int l,int r,int ql,int qr){
		if(ql>qr||!x) return 0;
		if(ql<=l&&r<=qr) return sum[x];
		int mid=l+r>>1;ll res=0;
		if(ql<=mid) res+=QuerySum(ls[x],l,mid,ql,qr);
		if(qr>mid) res+=QuerySum(rs[x],mid+1,r,ql,qr);
		return res;
	}
	int QueryNum(int x,int l,int r,int ql,int qr){
		if(ql>qr||!x) return 0;
		if(ql<=l&&r<=qr) return num[x];
		int mid=l+r>>1,res=0;
		if(ql<=mid) res+=QueryNum(ls[x],l,mid,ql,qr);
		if(qr>mid) res+=QueryNum(rs[x],mid+1,r,ql,qr);
		return res;
	}
}t1,t2;
ll Ans;
int main(){
	read(n),read(m),read(q);
	for(int i=1;i<=n;i++) t1.Modify(t1.root,0,M,a[i],a[i],1);
	for(int i=1;i<=m;i++) t2.Modify(t2.root,0,M,b[i],b[i],1);
//	for(int i=1;i<=n;i++){
//		Ans+=1ll*t2.QueryNum(t2.root,0,M,1,a[i])*a[i];
//		Ans+=1ll*t2.QuerySum(t2.root,0,M,a[i]+1,M);
//	}
	for(int i=1;i<=q;i++){
		int op,pos;ll v;
		read(op),read(pos),read(v);
		if(op==1){
			Ans-=1ll*t2.QueryNum(t2.root,0,M,0,a[pos])*a[pos];
			Ans-=1ll*t2.QuerySum(t2.root,0,M,a[pos]+1,M);
			t1.Modify(t1.root,0,M,a[pos],a[pos],-1);
			
			a[pos]=v;
			Ans+=1ll*t2.QueryNum(t2.root,0,M,0,a[pos])*a[pos];
			Ans+=1ll*t2.QuerySum(t2.root,0,M,a[pos]+1,M);
			t1.Modify(t1.root,0,M,a[pos],a[pos],1);
		}
		else{
			Ans-=1ll*t1.QueryNum(t1.root,0,M,0,b[pos])*b[pos];
			Ans-=1ll*t1.QuerySum(t1.root,0,M,b[pos]+1,M);
			t2.Modify(t2.root,0,M,b[pos],b[pos],-1);
			b[pos]=v;
			Ans+=1ll*t1.QueryNum(t1.root,0,M,0,b[pos])*b[pos];
			Ans+=1ll*t1.QuerySum(t1.root,0,M,b[pos]+1,M);
			t2.Modify(t2.root,0,M,b[pos],b[pos],1);
		}
		write(Ans),putchar('\n');
	}
	return 0;
}