#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

bool valid(int left,int right){
  if(left==right)return false;
  if(left==5||right==5) return false;
  if((left==2||left==8)&&(right==1||right==4||right==7))
    return false;
  if((left==6||left==9||left==3)
     &&(right==2||right==8||right==1||right==4||right==7))
    return false;
  return true;
}

const int INF=100000000;

int main(){

  string str;
  while(cin>>str&&str!="#"){
    int n=str.size();
    int minCnt=INF;
    int dp[2][11][11][2];
    for(int i = 1; i <= 9; i++){
      for(int j = 1; j <= 9; j++){
	if(valid(i,j)){
	  dp[0][i][j][0]=0;
	  dp[0][i][j][1]=0;
	}
	else{
	  dp[0][i][j][0]=INF;
	  dp[0][i][j][1]=INF;
	}
	dp[1][i][j][0]=INF;
	dp[1][i][j][1]=INF;
      }
    }
    // Å©çÅãÜÅ[vðñ·
    for(int i = 0; i < str.size(); i++){
      for(int j = 0; j < 11; j++)
	for(int k = 0; k < 11; k++)
	  dp[(i+1)%2][j][k][0]=dp[(i+1)%2][j][k][1]=INF;
      for(int j = 1; j <= 9; j++){
	for(int k = 1; k <= 9; k++){
	  // ¶«ªj,E«ªkÉ éóÔ©çJÚ·é
	  
	  if(!valid(j,k))
	    continue;
	  // if(dp[(i+1)%2][str[i]-'0'][k][1]<0
	  //    ||dp[(i+1)%2][str[i]-'0'][k][1]<0){
	  //   cout<<i<<" "<<j<<" "<<k<<endl;
	  // }
	  // ¡ñg¤«
	  if(valid(j,str[i]-'0')){
	    dp[(i+1)%2][j][str[i]-'0'][0]
	      =min(dp[(i+1)%2][j][str[i]-'0'][0]
		   ,dp[i%2][j][k][1]);
	    dp[(i+1)%2][j][str[i]-'0'][0]
	      =min(dp[(i+1)%2][j][str[i]-'0'][0]
		   ,dp[i%2][j][k][0]+1);
	  }
	  if(valid(str[i]-'0',k)){
	    dp[(i+1)%2][str[i]-'0'][k][1]
	      =min(dp[(i+1)%2][str[i]-'0'][k][1]
		   ,dp[i%2][j][k][1]+1);
	    dp[(i+1)%2][str[i]-'0'][k][1]
	      =min(dp[(i+1)%2][str[i]-'0'][k][1]
		   ,dp[i%2][j][k][0]);
	  }
	}
      }
    }
    for(int i = 1; i <= 9; i++)
      for(int j = 1; j <= 9; j++)
	for(int l = 0; l < 2; l++)
	  minCnt=min(minCnt,dp[n%2][i][j][l]);
    cout<<minCnt<<endl;
  }
  return 0;
}