import std.stdio;
import std.ascii;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.random;
import std.regex;
import std.typecons;

string s;

bool input() {
    s = readln.chomp;
    if (s == "#") return false;
    return true;
}

const INF = int.max / 4;
int check(int l, int r, int lr) {
    const LEFT = 0, RIGHT = 1;
    int[int] D = [
        1: 0, 4: 0, 7: 0,
        2: 1, 5: 1, 8: 1,
        3: 2, 6: 2, 9: 2
    ];
    int ret = 0;
    for (int i = 0; i < s.length; i++) {
        auto c = cast(int)(s[i] - '0');
        if (lr == LEFT) {
            if (D[r] < D[c]) {
                r = c;
                ret++;
            } else {
                l = c;
                lr = RIGHT;
            }
        } else {
            if (D[c] < D[l]) {
                l = c;
                ret++;
            } else {
                r = c;
                lr = LEFT;
            }
        }
    }
    return ret;
}

void solve() {
    int ans = INF;
    for (int l = 1; l <= 9; l++) {
        for (int r = 1; r <= 9; r++) {
            for (int lr = 0; lr < 2; lr++) {
                ans = min(ans, check(l, r, lr));
            }
        }
    }
    writeln(ans);
}

void main() {
    while (input) solve;
}