#include "bits/stdc++.h"
using namespace std;
#define ll long long
using namespace std;

int main() {
	cin.tie(0); ios::sync_with_stdio(false);
	string str;
	int ans;
	int foot[10] = { -1,1,2,3,1,-1,3,1,2,3 };
	while (cin >> str) {
		if (str == "#") break;
		ans = (1 << 30);
		int l = 1, r = 3;
		int next = 0;  // 0 : l   ,   1 : r
		int cnt = 0;
		for (int i = 0; i < str.length();i++) {
			//cout << "l = " << l << " / r = " << r << endl;
			int key = str[i] - '0';
			if (next == 0) {
				if (foot[key] <= r) {
					l = foot[key];
					next = 1;
				}
				else {
					r = foot[key];
					cnt++;
				}
			}
			else {
				if (foot[key] >= l) {
					r = foot[key];
					next = 0;
				}
				else {
					l = foot[key];
					cnt++;
				}
			}
		}
		ans = min(ans, cnt);

		l = 1, r = 3;
		next = 1;
		cnt = 0;
		for (int i = 0; i < str.length();i++) {
			//cout << "l = " << l << " / r = " << r << endl;
			int key = str[i] - '0';
			if (next == 0) {
				if (foot[key] <= r) {
					l = foot[key];
					next = 1;
				}
				else {
					r = foot[key];
					cnt++;
				}
			}
			else {
				if (foot[key] >= l) {
					r = foot[key];
					next = 0;
				}
				else {
					l = foot[key];
					cnt++;
				}
			}
		}

		ans = min(ans, cnt);
		cout << ans << endl;
	}
}