#include <algorithm>
#include <iostream>
#define INF (1e9)
using namespace std;

int main(){
  int d[10]={0,0,1,2,0,-1,2,0,1,2},cnt,ans,c;
  string s;
  while(1){
    cin>>s;
    if(s=="#") break;
    ans=INF;
    for(int i=0;i<2;i++){
      c=i;
      cnt=0;
      for(int j=1;j<s.size();j++){
	int k=s[j-1]-'0',t=s[j]-'0';
	if((!c&&d[k]>d[t])||(c&&d[k]<d[t])) cnt++;
	else c=!c;
      }
      ans=min(ans,cnt);
    }
    cout<<ans<<endl;
  }
  return 0;
}