Problem:
Problem B:
Step Step Evolution
Japanese video game company has developed the music video game called Step Step Evolution.
The gameplay of Step Step Evolution is very simple. Players stand on the dance platform, and
step on panels on it according to a sequence of arrows shown in the front screen.
There are eight types of direction arrows in the Step Step Evolution: UP, UPPER RIGHT,
RIGHT, LOWER RIGHT, DOWN, LOWER LEFT, LEFT, and UPPER LEFT. These direction
arrows will scroll upward from the bottom of the screen. When the direction arrow overlaps the
stationary arrow nearby the top, then the player must step on the corresponding arrow panel
on the dance platform. The figure below shows how the dance platform looks like.
Figure 1: the dance platform for Step Step Evolution
In the game, you have to obey the following rule:
Except for the beginning of the play, you must not press the arrow panel which is not
  correspond to the edit data.
The foot must stay upon the panel where it presses last time, and will not be moved until
  it’s going to press the next arrow panel.
The left foot must not step on the panel which locates at right to the panel on which the
right foot rests. Conversely, the right foot must not step on the panel which locates at left
to the panel on which the left foot rests.
As for the third condition, the following figure shows the examples of the valid and invalid
footsteps.
Figure 2: the examples of valid and invalid footsteps
The first one (left foot for LEFT, right foot for DOWN) and the second one (left foot for LOWER
LEFT, right foot for UPPER LEFT) are valid footstep. The last one (left foot for RIGHT, right
foot for DOWN) is invalid footstep.
Note that, at the beginning of the play, the left foot and right foot can be placed anywhere at
the arrow panel. Also, you can start first step with left foot or right foot, whichever you want.
To play this game in a beautiful way, the play style called “ Natural footstep style” is commonly
known among talented players. “Natural footstep style” is the style in which players make steps
by the left foot and the right foot in turn. However, when a sequence of arrows is difficult,
players are sometimes forced to violate this style.
Now, your friend has created an edit data (the original sequence of direction arrows to be pushed)
for you. You are interested in how many times you have to violate “Natural footstep style” when
you optimally played with this edit data. In other words, what is the minimum number of times
you have to step on two consecutive arrows using the same foot?
Input
The input consists of several detasets. Each dataset is specified by one line containing a sequence
of direction arrows. Directions are described by numbers from 1 to 9, except for 5. The figure
below shows the correspondence between numbers and directions.
You may assume that the length of the sequence is between 1 and 100000, inclusive. Also, arrows
of the same direction won’t appear consecutively in the line
...(truncated)

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using a2 = array<ll, 2>;
using a3 = array<ll, 3>;

bool chmin(auto& a, const auto& b) { return a > b ? a = b, 1 : 0; }
bool chmax(auto& a, const auto& b) { return a < b ? a = b, 1 : 0; }

ll mod = 998244353;
const ll INF = 1e18;

ifstream in;
ofstream out;

int main(int argc, char** argv) {
    ios::sync_with_stdio(false);
    cin.tie(0);

    if(argc > 2) {
        in.open(argv[1]);
        cin.rdbuf(in.rdbuf());
        out.open(argv[2]);
        cout.rdbuf(out.rdbuf());
    }

    while(1) {
        vector<ll> mp(10);
        mp[1] = 0;
        mp[2] = 1;
        mp[3] = 2;
        mp[4] = 0;
        mp[5] = 1;
        mp[6] = 2;
        mp[7] = 0;
        mp[8] = 1;
        mp[9] = 2;

        string s;
        cin >> s;
        if(s == "#") break;

        ll ans = INF;
        vector<vector<a2>> dp(10, vector<a2>(10, {INF, INF}));
        dp[5][5] = {0, 0};
        for(auto& x : s) {
            ll num = x - '0';
            vector<vector<a2>> dp2(10, vector<a2>(10, {INF, INF}));
            for(int i = 0; i < 10; i++) {
                for(int j = 0; j < 10; j++) {
                    if(mp[i] <= mp[num] || i == 5) {
                        chmin(dp2[i][num][1], dp[i][j][0]);
                        chmin(dp2[i][num][1], dp[i][j][1] + 1);
                    }
                    if(mp[num] <= mp[i] || i == 5) {
                        chmin(dp2[num][i][0], dp[j][i][1]);
                        chmin(dp2[num][i][0], dp[j][i][0] + 1);
                    }
                }
            }
            swap(dp, dp2);

            ll res = INF;
            for(int i = 0; i < 10; i++) {
                for(int j = 0; j < 10; j++) {
                    chmin(res, dp[i][j][0]);
                    chmin(res, dp[i][j][1]);
                }
            }
            // cerr << res << endl;
        }

        for(int i = 0; i < 10; i++) {
            for(int j = 0; j < 10; j++) {
                chmin(ans, dp[i][j][0]);
                chmin(ans, dp[i][j][1]);
            }
        }
        cout << ans << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 210
memory_kb: 3712
final_rank: 9

code_snippet: #include<bits/stdc++.h>
// #include<atcoder/all>
// #include<boost/multiprecision/cpp_int.hpp>

using namespace std;
// using namespace atcoder;
// using bint = boost::multiprecision::cpp_int;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<ll>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using ve = vector<vector<int>>;
using vb = vector<bool>;
using vvb = vector<vb>;
#define rep(i,s,n) for(ll i = (ll)s;i < (ll)n;i++)
#define rrep(i,l,r) for(ll i = r-1;i >= l;i--)
#define ALL(x) (x).begin(),(x).end()
#define sz(c) ((ll)(c).size())
#define LB(A,x) (int)(lower_bound(A.begin(),A.end(),x)-A.begin())
#define UB(A,x) (int)(upper_bound(A.begin(),A.end(),x)-A.begin())
// #define MOD 1000000007
#define MOD 998244353
template<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<typename T>ostream&operator<<(ostream&os,vector<T>&v){for(int i = 0;i < v.size();i++)os<<v[i]<<(i+1!=v.size()?" ":"");return os;}
template<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}
template<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.first<<" "<<p.second;return os;}
template<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}
template<typename T> inline bool chmax(T &a,T b){if(a < b){a = b;return true;}return false;}
template<typename T> inline bool chmin(T &a,T b){if(a > b){a = b;return true;}return false;}
ld dist(ld x1,ld y1,ld x2, ld y2){return sqrtl((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}


int main(){
  
  ios_base::sync_with_stdio(0), cin.tie(0);
  auto valid = [](int i,int j)->bool{
    if(i == 0 || j == 0 || i == j)return false;
    i = (i+2)%3;j = (j+2)%3;
    return i <= j;
  };
  while(1){
    string s;cin >> s;
    if(s[0] == '#')break;
    vvvi dp(10,vvi(10,vi(2,MOD)));
    rep(i,0,10)rep(j,0,10)rep(k,0,2)if(valid(i,j))dp[i][j][k] = 0;
    rep(_,0,sz(s)){
      vvvi DP(10,vvi(10,vi(2,MOD)));
      int nu = s[_]-'0';
      rep(i,0,10)rep(j,0,10)rep(k,0,2){
        if(dp[i][j][k] == MOD)continue;
        if(valid(i,nu))chmin(DP[i][nu][1],dp[i][j][k] + (k == 1));
        if(valid(nu,j))chmin(DP[nu][j][0],dp[i][j][k] + (k == 0));
      }
      swap(dp,DP);
    }
    ll res = MOD;
    rep(i,0,10)rep(j,0,10)rep(k,0,2)chmin(res,dp[i][j][k]);
    cout << res << "\n";
  }
  

  return 0;
}
accuracy: 1
time_ms: 1490
memory_kb: 3428
final_rank: 17

code_snippet: #include <iostream>
#include <iomanip>
#include <cassert>
#include <vector>
#include <algorithm>
#include <utility>
#include <numeric>
#include <tuple>
#include <ranges>
// #include "Src/Number/IntegerDivision.hpp"
// #include "Src/Utility/BinarySearch.hpp"
// #include "Src/Sequence/CompressedSequence.hpp"
// #include "Src/Sequence/RunLengthEncoding.hpp"
// #include "Src/Algebra/Group/AdditiveGroup.hpp"
// #include "Src/DataStructure/FenwickTree/FenwickTree.hpp"
// #include "Src/DataStructure/SegmentTree/SegmentTree.hpp"
// #include "Src/DataStructure/DisjointSetUnion/DisjointSetUnion.hpp"
// using namespace zawa;
// #include "atcoder/modint"
// using mint = atcoder::modint998244353;
// #include <array>
// #include <bit>
// #include <bitset>
// #include <climits>
// #include <cmath>
// #include <set>
// #include <unordered_set>
// #include <map>
// #include <unordered_map>
// #include <optional>
// #include <queue>
// #include <stack>
// #include <random>
using namespace std;
template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    for (int i = 0 ; i < ssize(v) ; i++)
        os << v[i] << (i + 1 == ssize(v) ? "" : " ");
    return os;
}
int solve(const string& s) {
    const int INF = (int)1e9;
    vector dp(2, vector(9, vector<int>(9, INF)));
    for (int i = 0 ; i < 9 ; i++)
        for (int j = 0 ; j < 9 ; j++)
            if (i % 3 <= j % 3)
                dp[0][i][j] = dp[1][i][j] = 0;
    for (char c : s) {
        const int v = c - '1';
        vector nxt(2, vector(9, vector<int>(9, INF)));
        for (int i = 0 ; i < 2 ; i++)
            for (int l = 0 ; l < 9 ; l++)
                for (int r = 0 ; r < 9 ; r++)
                    if (dp[i][l][r] != INF) {
                        assert(l % 3 <= r % 3);
                        if (v % 3 <= r % 3)
                            nxt[0][v][r] = min(nxt[0][v][r], dp[i][l][r] + (i == 0));
                        if (l % 3 <= v % 3)
                            nxt[1][l][v] = min(nxt[1][l][v], dp[i][l][r] + (i == 1));
                    }
        dp = move(nxt);
    }
    int ans = INF;
    for (int i = 0 ; i < 2 ; i++)
        for (int j = 0 ; j < 9 ; j++)
            for (int k = 0 ; k < 9 ; k++)
                ans = min(ans, dp[i][j][k]);
    return ans;
}
int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    while (true) {
        string s;
        cin >> s;
        if (s == "#")
            break;
        cout << solve(s) << '\n';
    }
}
accuracy: 1
time_ms: 280
memory_kb: 3712
final_rank: 10

code_snippet: #include <string>
#include <vector>
#include <iostream>
using namespace std;

const int INF = 1012345678;

int solve(const string& S) {
	int N = S.size();
	vector<int> A(N);
	for (int i = 0; i < N; i++) {
		int x = S[i] - '0';
		A[i] = (x - 1) % 3;
	}
	vector<vector<bool> > vis(N, vector<bool>(18, false));
	vector<vector<int> > dp(N, vector<int>(18, -1));
	auto calc = [&](auto& self, int pos, int a, int b, int f) -> int {
		if (pos == N) {
			return 0;
		}
		int h = (a * 3 + b) * 2 + f;
		if (vis[pos][h]) {
			return dp[pos][h];
		}
		int res = INF;
		if (A[pos] <= b) {
			int subres = self(self, pos + 1, A[pos], b, 0);
			res = min(res, subres + int(f == 0));
		}
		if (A[pos] >= a) {
			int subres = self(self, pos + 1, a, A[pos], 1);
			res = min(res, subres + int(f == 1));
		}
		vis[pos][h] = true;
		dp[pos][h] = res;
		return res;
	};
	int ans = INF;
	for (int i = 0; i < 3; i++) {
		for (int j = i; j < 3; j++) {
			for (int k = 0; k < 2; k++) {
				int res = calc(calc, 0, i, j, k);
				ans = min(ans, res);
			}
		}
	}
	return ans;
}

int main() {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	while (true) {
		string S;
		cin >> S;
		if (S == "#") {
			break;
		}
		int ans = solve(S);
		cout << ans << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 31720
final_rank: 12

code_snippet: #include <string>
#include <vector>
#include <iostream>
using namespace std;

const int INF = 1012345678;

int solve(const string& S) {
	int N = S.size();
	vector<int> A(N);
	for (int i = 0; i < N; i++) {
		int x = S[i] - '0';
		A[i] = (x - 1) % 3;
	}
	vector<vector<bool> > vis(N, vector<bool>(18, false));
	vector<vector<int> > dp(N, vector<int>(18, -1));
	auto calc = [&](auto& self, int pos, int a, int b, int f, int t) -> int {
		if (pos == N) {
			return 0;
		}
		int h = (a * 3 + b) * 2 + f;
		if (vis[pos][h]) {
			return dp[pos][h];
		}
		int res = INF;
		if (A[pos] <= b) {
			int subres = self(self, pos + 1, A[pos], b, 0, t + int(f == 0));
			res = min(res, subres + int(f == 0));
		}
		if (A[pos] >= a) {
			int subres = self(self, pos + 1, a, A[pos], 1, t + int(f == 1));
			res = min(res, subres + int(f == 1));
		}
		vis[pos][h] = true;
		dp[pos][h] = res;
		return res;
	};
	int ans = INF;
	for (int i = 0; i < 3; i++) {
		for (int j = i; j < 3; j++) {
			for (int k = 0; k < 2; k++) {
				int res = calc(calc, 0, i, j, k, 0);
				ans = min(ans, res);
			}
		}
	}
	return ans;
}

int main() {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	while (true) {
		string S;
		cin >> S;
		if (S == "#") {
			break;
		}
		int ans = solve(S);
		cout << ans << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 33404
final_rank: 13

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

#include <unistd.h>

namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf


uint32_t pil = 0, pir = 0, por = 0;

struct Pre {
    char num[10000][4];
    constexpr Pre() : num() {
        for (int i = 0; i < 10000; i++) {
            int n = i;
            for (int j = 3; j >= 0; j--) {
                num[i][j] = n % 10 | '0';
                n /= 10;
            }
        }
    }
} constexpr pre;

inline void load() {
    memmove(ibuf, ibuf + pil, pir - pil);
    pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
    pil = 0;
    if (pir < SZ)
        ibuf[pir++] = '\n';
}

inline void flush() {
    fwrite(obuf, 1, por, stdout);
    por = 0;
}

void rd(char &c) {
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
}

void rd(string &x) {
    x.clear();
    char c;
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
    do {
        x += c;
        if (pil == pir)
            load();
        c = ibuf[pil++];
    } while (!isspace(c));
}

template <typename T> void rd_real(T &x) {
    string s;
    rd(s);
    x = stod(s);
}

template <typename T> void rd_integer(T &x) {
    if (pil + 100 > pir)
        load();
    char c;
    do
        c = ibuf[pil++];
    while (c < '-');
    bool minus = 0;
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (c == '-') {
            minus = 1, c = ibuf[pil++];
        }
    }
    x = 0;
    while ('0' <= c) {
        x = x * 10 + (c & 15), c = ibuf[pil++];
    }
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (minus)
            x = -x;
    }
}

void rd(int &x) {
    rd_integer(x);
}
void rd(ll &x) {
    rd_integer(x);
}
void rd(i128 &x) {
    rd_integer(x);
}
void rd(uint &x) {
    rd_integer(x);
}
void rd(ull &x) {
    rd_integer(x);
}
void rd(u128 &x) {
    rd_integer(x);
}
void rd(double &x) {
    rd_real(x);
}
void rd(long double &x) {
    rd_real(x);
}

template <class T, class U> void rd(pair<T, U> &p) {
    return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T> void rd_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
        auto &x = std::get<N>(t);
        rd(x);
        rd_tuple<N + 1>(t);
    }
}
template <class... T> void rd(tuple<T...> &tpl) {
    rd_tuple(tpl);
}

template <size_t N = 0, typename T> void rd(array<T, N> &x) {
    for (auto &d : x)
        rd(d);
}
template <class T> void rd(vector<T> &x) {
    for (auto &d : x)
        rd(d);
}

void read() {}
template <class H, class... T> void read(H &h, T &...t) {
    rd(h), read(t...);
}

void wt(const char c) {
    if (por == SZ)
        flush();
    obuf[por++] = c;
}
void wt(const string s) {
    for (char c : s)
        wt(c);
}
void wt(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++)
        wt(s[i]);
}

template <typename T> void wt_integer(T x) {
    if (por > SZ - 100)
        flush();
    if (x < 0) {
        obuf[por++] = '-', x = -x;
    }
    int outi;
    for (outi = 96; x >= 10000; outi -= 4) {
        memcpy(out + outi, pre.num[x % 10000], 4);
        x /= 10000;
    }
    if (x >= 1000) {
        memcpy(obuf + por, pre.num[x], 4);
        por += 4;
    } else if (x >= 100) {
        memcpy(obuf + por, pre.num[x] + 1, 3);
        por += 3;
    } else if (x >= 10) {
        int q = (x * 103) >> 10;
        obuf[por] = q | '0';
        obuf[por + 1] = (x - q * 10) | '0';
        por += 2;
    } else
        obuf[por++] = x | '0';
    memcpy(obuf + por, out + outi + 4, 96 - outi);
    por += 96 - outi;
}

template <typename T> void wt_real(T x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << double(x);
    string s = oss.str();
    wt(s);
}

void wt(int x) {
    wt_integer(x);
}
void wt(ll x) {
    wt_integer(x);
}
void wt(i128 x) {
    wt_integer(x);
}
void wt(uint x) {
    wt_integer(x);
}
void wt(ull x) {
    wt_integer(x);
}
void wt(u128 x) {
    wt_integer(x);
}
void wt(double x) {
    wt_real(x);
}
void wt(long double x) {
    wt_real(x);
}

template <class T, class U> void wt(const pair<T, U> val) {
    wt(val.first);
    wt(' ');
    wt(val.second);
}
template <size_t N = 0, typename T> void wt_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
        if constexpr (N > 0) {
            wt(' ');
        }
        const auto x = std::get<N>(t);
        wt(x);
        wt_tuple<N + 1>(t);
    }
}
template <class... T> void wt(tuple<T...> tpl) {
    wt_tuple(tpl);
}
template <class T, size_t S> void wt(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}
template <class T> void wt(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}

void print() {
    wt('\n');
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    wt(head);
    if (sizeof...(Tail))
        wt(' ');
    print(forward<Tail>(tail)...);
}
void __attribute__((destructor)) _d() {
    flush();
}
} // namespace fastio


using fastio::flush;
using fastio::print;
using fastio::read;

inline void first(bool i = true) {
    print(i ? "first" : "second");
}
inline void Alice(bool i = true) {
    print(i ? "Alice" : "Bob");
}
inline void Takahashi(bool i = true) {
    print(i ? "Takahashi" : "Aoki");
}
inline void yes(bool i = true) {
    print(i ? "yes" : "no");
}
inline void Yes(bool i = true) {
    print(i ? "Yes" : "No");
}
inline void No() {
    print("No");
}
inline void YES(bool i = true) {
    print(i ? "YES" : "NO");
}
inline void NO() {
    print("NO");
}
inline void Yay(bool i = true) {
    print(i ? "Yay!" : ":(");
}
inline void Possible(bool i = true) {
    print(i ? "Possible" : "Impossible");
}
inline void POSSIBLE(bool i = true) {
    print(i ? "POSSIBLE" : "IMPOSSIBLE");
}

/**
 * @brief Fast IO
 */

int main() {
while(1) {
    string S;
    cin >> S;
    if (S == "#") return 0;
    int N = S.size();
    vector<int> A(N);
    rep(i,0,N) {
        A[i] = (S[i]-'0'+2) % 3;
    }
    int DP[100000][2][3][3] = {};
    rep(i,0,N) rep(j,0,2) rep(k,0,3) rep(l,0,3) DP[i][j][k][l] = inf;
    DP[0][0][A[0]][2] = DP[0][1][0][A[0]] = 0;
    rep(i,0,N-1) {
        rep(j,0,2) {
            rep(k,0,3) {
                rep(l,0,3) {
                    if (DP[i][j][k][l] == inf) continue;
                    if (j == 0) {
                        if (A[i+1] <= l) chmin(DP[i+1][0][A[i+1]][l],DP[i][j][k][l]+1);
                        if (k <= A[i+1]) chmin(DP[i+1][1][k][A[i+1]],DP[i][j][k][l]);
                    }
                    else {
                        if (A[i+1] <= l) chmin(DP[i+1][0][A[i+1]][l],DP[i][j][k][l]);
                        if (k <= A[i+1]) chmin(DP[i+1][1][k][A[i+1]],DP[i][j][k][l]+1);
                    }
                }
            }
        }
    }
    int ANS = inf;
    rep(j,0,2) {
        rep(k,0,3) {
            rep(l,0,3) {
                chmin(ANS, DP[N-1][j][k][l]);
            }
        }
    }
    cout << ANS << endl;
}
}
accuracy: 1
time_ms: 30
memory_kb: 11056
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, a, b) for(ll i = a; i < b; ++i)
#define rrep(i, a, b) for(ll i = a; i >= b; --i)
constexpr ll inf = 4e18;
struct SetupIO {
    SetupIO() {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout << fixed << setprecision(30);
    }
} setup_io;
int main(void) {
    while(1) {
        string s;
        cin >> s;
        if(s == "#") break;
        int n = s.size();
        vector<vector<int>> dp(n, vector<int>(2));
        rep(i, 1, n) {
            int prev = (s[i - 1] - '1') % 3, cur = (s[i] - '1') % 3;
            if(cur < prev) {
                dp[i][0] = min(dp[i - 1][0] + 1, dp[i - 1][1]);
                dp[i][1] = dp[i - 1][1] + 1;
            } else if(cur == prev) {
                dp[i][0] = min(dp[i - 1][0] + 1, dp[i - 1][1]);
                dp[i][1] = min(dp[i - 1][0], dp[i - 1][1] + 1);
            } else {
                dp[i][0] = dp[i - 1][0] + 1;
                dp[i][1] = min(dp[i - 1][0], dp[i - 1][1] + 1);
            }
        }
        cout << min(dp[n - 1][0], dp[n - 1][1]) << '\n';
    }
}
accuracy: 1
time_ms: 20
memory_kb: 8776
final_rank: 2

code_snippet: // author: hanyu
#include <bits/stdc++.h>
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (true) {
    string s;
    cin >> s;
    if (s == "#") break;

    int n = s.size();

    vector<int> x(n);
    for (int i = 0; i < n; i++) {
      if ((s[i] - '0') % 3 == 1) x[i] = 0;
      else if ((s[i] - '0') % 3 == 2) x[i] = 1;
      else x[i] = 2;
    }

    vector<vector<int>> dp(n, vector<int>(2, 1e9));
    dp[0][0] = dp[0][1] = 0;
    for (int i = 0; i < n - 1; i++) {
      if (x[i] == x[i + 1]) {
        dp[i + 1][0] = min(dp[i][0] + 1, dp[i][1]);
        dp[i + 1][1] = min(dp[i][0], dp[i][1] + 1);
      }
      else if (x[i] < x[i + 1]) {
        dp[i + 1][0] = min(dp[i][0] + 1, dp[i][1]);
        dp[i + 1][1] = dp[i][1] + 1;
      }
      else {
        dp[i + 1][0] = dp[i][0] + 1;
        dp[i + 1][1] = min(dp[i][0], dp[i][1] + 1);
      }
    }

    cout << min(dp[n - 1][0], dp[n - 1][1]) << endl;
  }
}
accuracy: 1
time_ms: 20
memory_kb: 9152
final_rank: 3

code_snippet: #pragma region template
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#ifdef __LOCAL
 #include <debug>
#else
 #define debug(...) void(0)
#endif

#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()

template<typename T>
istream& operator>>(istream&is,vector<T>&v){
  for(T&p:v)is>>p;
  return is;
}
template<typename T>
ostream& operator<<(ostream&os,const vector<T>&v){
  if(&os==&cerr)os<<"[";
  for(int i=0;i<v.size();i++){
    os<<v[i];
    if(i+1<v.size())os<<(&os==&cerr?",":" ");
  }
  if(&os==&cerr)os<<"]";
  return os;
}
#pragma endregion template

const int INF=1e9+7;
template<typename T,typename ...Args>
auto make_vector(T x,int arg,Args ...args){
  if constexpr(sizeof...(args)==0)return vector<T>(arg,x);
  else return vector(arg,make_vector<T>(x,args...));
}
template<typename T>
bool chmin(T &a,T b){
  return (a>b&&(a=b,true));
}

int main(){
  string s;
  while(cin>>s,s!="#"){
    int n=s.size();

    auto f=[](char c){
      return (c-'1')%3;
    };
    
    auto dp=make_vector<int>(0,3,3,2);//左,右,最後が左？
    REP(i,3)REP(j,3)REP(k,2)if(i>j)dp[i][j][k]=INF;
    for(const char c:s){
      int v=f(c);
      auto nxt=make_vector<int>(INF,3,3,2);
      REP(i,3)REP(j,3)REP(k,2){
        //左にv
        if(v<=j)chmin(nxt[v][j][1],dp[i][j][k]+(k==1));
        //右
        if(v>=i)chmin(nxt[i][v][0],dp[i][j][k]+(k==0));
      }
      dp=nxt;
    }
    int ans=INF;
    REP(i,3)REP(j,3)REP(k,2)chmin(ans,dp[i][j][k]);
    cout<<ans<<"\n";
  }
}
accuracy: 1
time_ms: 180
memory_kb: 3468
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vvvvll = vector<vvvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vd = vector<double>;
using vvd = vector<vd>;
using vvvd = vector<vvd>;
#define all(A) A.begin(),A.end()
#define ALL(A) A.begin(),A.end()
#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)
using pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;

template<class T>
bool chmax(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p < q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}

template<class T>
bool chmin(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p > q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}

ll gcd(ll(a), ll(b)) {
    if (b == 0)return a;
    ll c = a;
    while (a % b != 0) {
        c = a % b;
        a = b;
        b = c;
    }
    return b;
}

vector<ll> fact, factinv, inv;
ll mod = 1e9 + 7;
void prenCkModp(ll n) {
    fact.resize(n + 5);
    factinv.resize(n + 5);
    inv.resize(n + 5);
    fact.at(0) = fact.at(1) = 1;
    factinv.at(0) = factinv.at(1) = 1;
    inv.at(1) = 1;
    for (ll i = 2; i < n + 5; i++) {
        fact.at(i) = (fact.at(i - 1) * i) % mod;
        inv.at(i) = mod - (inv.at(mod % i) * (mod / i)) % mod;
        factinv.at(i) = (factinv.at(i - 1) * inv.at(i)) % mod;
    }

}
ll nCk(ll n, ll k) {
    if (k < 0)return 0;
    if (n < k) return 0;
    return fact.at(n) * (factinv.at(k) * factinv.at(n - k) % mod) % mod;
}



int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    while(1){
        string S;
        cin>>S;
        if(S=="#")return 0;
        ll N=S.size();
        vvvvll DP(N+1,vvvll(3,vvll(3,vll(2,1e18))));
        rep(i,3)rep(j,3)rep(p,2)DP[0][i][j][p]=0;
        rep(n,N){
            ll d=S[n]-'0';
            d=(d+2)%3;
            rep(i,3){//L
                rep(j,3){//R
                    if(i<=d){
                        chmin(DP[n+1][i][d][1],DP[n][i][j][0]);
                    }
                    else{
                        chmin(DP[n+1][d][j][0],DP[n][i][j][0]+1);
                    }

                    if(d<=j){
                        chmin(DP[n+1][d][j][0],DP[n][i][j][1]);
                    }
                    else{
                        chmin(DP[n+1][i][d][1],DP[n][i][j][1]+1);
                    }
                }
            }
        }
        ll an=1e18;
        rep(i,3)rep(j,3)rep(p,2)chmin(an,DP[N][i][j][p]);
        cout<<an<<endl;
    }

}
accuracy: 1
time_ms: 360
memory_kb: 64844
final_rank: 15

code_snippet: #include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define drep(i,j,n) for(int i=0;i<(int)(n-1);i++)for(int j=i+1;j<(int)(n);j++)
#define trep(i,j,k,n) for(int i=0;i<(int)(n-2);i++)for(int j=i+1;j<(int)(n-1);j++)for(int k=j+1;k<(int)(n);k++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yes(ans) if(ans)printf("yes\n");else printf("no\n")
#define Yes(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YES(ans) if(ans)printf("YES\n");else printf("NO\n")
#define popcount(v) __builtin_popcount(v)
#define vector1d(type,name,...) vector<type>name(__VA_ARGS__)
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
#define vector4d(type,name,h,w,d,...) vector<vector<vector<vector<type>>>>name(h,vector<vector<vector<type>>>(w,vector<vector<type>>(d,vector<type>(__VA_ARGS__))))
using namespace std;
using ll = long long;
template<class T>using rpriority_queue = priority_queue<T, vector<T>, greater<T>>;
const int MOD=1000000007;
const int MOD2=998244353;
const int INF=1<<30;
const ll INF2=(ll)1<<60;
//入力系
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
//出力系
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
//デバッグ系
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
ll modinv(ll a, ll m) {ll b = m, u = 1, v = 0;while (b) {ll t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}
ll updivide(ll a,ll b){if(a%b==0) return a/b;else return (a/b)+1;}
template<class T> void chmax(T &a,const T b){if(b>a)a=b;}
template<class T> void chmin(T &a,const T b){if(b<a)a=b;}

int dp[100000][10][10][2];

int main(){
    string s;
    while(cin>>s){
        if(s=="#")return 0;
        int n=s.size();
        for(int i=0;i<n;i++){
            for(int j=0;j<10;j++){
                for(int k=0;k<10;k++){
                    dp[i][j][k][0]=dp[i][j][k][1]=-1;
                }
            }
        }
        auto f=[&](int v){
            if(v==1||v==4||v==7)return -1;
            if(v==2||v==8)return 0;
            return 1;
        };
        function<int(int,int,int,int)> rec=[&](int i,int l,int r,int pre){
            if(i>=n)return 0;
            if(dp[i][l][r][pre]!=-1)return dp[i][l][r][pre];
            int nxt=s[i]-'0',ans=INF;
            if(f(nxt)<=f(r))chmin(ans,rec(i+1,nxt,r,0)+(i!=0&&pre==0));
            if(f(nxt)>=f(l))chmin(ans,rec(i+1,l,nxt,1)+(i!=0&&pre==1));
            return dp[i][l][r][pre]=ans;
        };
        out(rec(0,1,3,0));
    }
}
accuracy: 1
time_ms: 160
memory_kb: 95580
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(x) x.begin(), x.end()

const int INF = 1 << 30;

const int dx[9] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
const int dy[9] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};

void solve(string &S) {
    int N = (int)S.size();
    vector<int> a(N);
    rep(i, N) a[i] = S[i] - '1';
    // 最初の足の配置と、どっちの足から動くかを全探索する
    int ans = INF;
    rep(l, 9) rep(r, 9) rep(k, 2) {
        int lx = dx[l], ly = dy[l], rx = dx[r], ry = dy[r];
        if (lx > rx) continue;
        int nex = k;
        int cur = 0;
        rep(i, N) {
            if (nex) {
                // 左足を動かす
                int nx = dx[a[i]], ny = dy[a[i]];
                if (nx > rx) {
                    // 右足を動かすことにする
                    cur++;
                    rx = nx, ry = ny;
                } else {
                    lx = nx, ly = ny;
                    nex ^= 1;
                }
            } else {
                // 右足を動かす
                int nx = dx[a[i]], ny = dy[a[i]];
                if (lx > nx) {
                    // 左足を動かすことにする
                    lx = nx, ly = ny;
                    cur++;
                } else {
                    rx = nx, ry = ny;
                    nex ^= 1;
                }
            }
        }
        ans = min(ans, cur);
    }
    cout << ans << '\n';
    return;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    string S;
    while (cin >> S, S != "#") solve(S);
    return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 3992
final_rank: 5

code_snippet: #line 1 "/Users/nok0/Documents/Programming/nok0/cftemp.hpp"
#include <bits/stdc++.h>
using namespace std;

#pragma region Macros
// rep macro
#define foa(v, a) for(auto &v : a)
#define REPname(a, b, c, d, e, ...) e
#define REP(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)
#define REP0(x) for(int i = 0; i < (x); ++i)
#define REP1(i, x) for(int i = 0; i < (x); ++i)
#define REP2(i, l, r) for(int i = (l); i < (r); ++i)
#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))
#define REPSname(a, b, c, ...) c
#define REPS(...) REPSname(__VA_ARGS__, REPS1, REPS0)(__VA_ARGS__)
#define REPS0(x) for(int i = 1; i <= (x); ++i)
#define REPS1(i, x) for(int i = 1; i <= (x); ++i)
#define RREPname(a, b, c, d, e, ...) e
#define RREP(...) RREPname(__VA_ARGS__, RREP3, RREP2, RREP1, RREP0)(__VA_ARGS__)
#define RREP0(x) for(int i = (x)-1; i >= 0; --i)
#define RREP1(i, x) for(int i = (x)-1; i >= 0; --i)
#define RREP2(i, r, l) for(int i = (r)-1; i >= (l); --i)
#define RREP3(i, r, l, c) for(int i = (r)-1; i >= (l); i -= (c))
#define RREPSname(a, b, c, ...) c
#define RREPS(...) RREPSname(__VA_ARGS__, RREPS1, RREPS0)(__VA_ARGS__)
#define RREPS0(x) for(int i = (x); i >= 1; --i)
#define RREPS1(i, x) for(int i = (x); i >= 1; --i)

// name macro
#define pb push_back
#define eb emplace_back
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define popcnt(x) __builtin_popcountll(x)
template <class T = int>
using V = std::vector<T>;
template <class T = int>
using VV = std::vector<std::vector<T>>;
template <class T>
using pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;
using ll = long long;
using ld = long double;
using int128 = __int128_t;
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;

// input macro
template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
	is >> p.first >> p.second;
	return is;
}
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
	for(T &i : v) is >> i;
	return is;
}
std::istream &operator>>(std::istream &is, __int128_t &a) {
	std::string s;
	is >> s;
	__int128_t ret = 0;
	for(int i = 0; i < s.length(); i++)
		if('0' <= s[i] and s[i] <= '9')
			ret = 10 * ret + s[i] - '0';
	a = ret * (s[0] == '-' ? -1 : 1);
	return is;
}
namespace scanner {
void scan(int &a) { std::cin >> a; }
void scan(long long &a) { std::cin >> a; }
void scan(std::string &a) { std::cin >> a; }
void scan(char &a) { std::cin >> a; }
void scan(char a[]) { std::scanf("%s", a); }
void scan(double &a) { std::cin >> a; }
void scan(long double &a) { std::cin >> a; }
template <class T, class U>
void scan(std::pair<T, U> &p) { std::cin >> p; }
template <class T>
void scan(std::vector<T> &a) { std::cin >> a; }
void INPUT() {}
template <class Head, class... Tail>
void INPUT(Head &head, Tail &... tail) {
	scan(head);
	INPUT(tail...);
}
}  // namespace scanner
#define VEC(type, name, size)     \
	std::vector<type> name(size); \
	scanner::INPUT(name)
#define VVEC(type, name, h, w)                                    \
	std::vector<std::vector<type>> name(h, std::vector<type>(w)); \
	scanner::INPUT(name)
#define INT(...)     \
	int __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LL(...)            \
	long long __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define STR(...)             \
	std::string __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define CHAR(...)     \
	char __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define DOUBLE(...)     \
	double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LD(...)              \
	long double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)

// output-macro
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
	os << p.first << " " << p.second;
	return os;
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &a) {
	for(int i = 0; i < int(a.size()); ++i) {
		if(i) os << " ";
		os << a[i];
	}
	return os;
}
std::ostream &operator<<(std::ostream &dest, __int128_t &value) {
	std::ostream::sentry s(dest);
	if(s) {
		__uint128_t tmp = value < 0 ? -value : value;
		char buffer[128];
		char *d = std::end(buffer);
		do {
			--d;
			*d = "0123456789"[tmp % 10];
			tmp /= 10;
		} while(tmp != 0);
		if(value < 0) {
			--d;
			*d = '-';
		}
		int len = std::end(buffer) - d;
		if(dest.rdbuf()->sputn(d, len) != len) {
			dest.setstate(std::ios_base::badbit);
		}
	}
	return dest;
}
template <class T>
void print(const T a) { std::cout << a << '\n'; }
template <class Head, class... Tail>
void print(Head H, Tail... T) {
	std::cout << H << ' ';
	print(T...);
}
template <class T>
void printel(const T a) { std::cout << a << '\n'; }
template <class T>
void printel(const std::vector<T> &a) {
	for(const auto &v : a)
		std::cout << v << '\n';
}
template <class Head, class... Tail>
void printel(Head H, Tail... T) {
	std::cout << H << '\n';
	printel(T...);
}
void Yes(const bool b = true) { std::cout << (b ? "Yes\n" : "No\n"); }
void No() { std::cout << "No\n"; }
void YES(const bool b = true) { std::cout << (b ? "YES\n" : "NO\n"); }
void NO() { std::cout << "NO\n"; }
void err(const bool b = true) {
	if(b) {
		std::cout << "-1\n", exit(0);
	}
}

//debug macro
namespace debugger {
template <class T>
void view(const std::vector<T> &a) {
	std::cerr << "{ ";
	for(const auto &v : a) {
		std::cerr << v << ", ";
	}
	std::cerr << "\b\b }";
}
template <class T>
void view(const std::vector<std::vector<T>> &a) {
	std::cerr << "{\n";
	for(const auto &v : a) {
		std::cerr << "\t";
		view(v);
		std::cerr << "\n";
	}
	std::cerr << "}";
}
template <class T, class U>
void view(const std::vector<std::pair<T, U>> &a) {
	std::cerr << "{\n";
	for(const auto &p : a) std::cerr << "\t(" << p.first << ", " << p.second << ")\n";
	std::cerr << "}";
}
template <class T, class U>
void view(const std::map<T, U> &m) {
	std::cerr << "{\n";
	for(const auto &p : m) std::cerr << "\t[" << p.first << "] : " << p.second << "\n";
	std::cerr << "}";
}
template <class T, class U>
void view(const std::pair<T, U> &p) { std::cerr << "(" << p.first << ", " << p.second << ")"; }
template <class T>
void view(const std::set<T> &s) {
	std::cerr << "{ ";
	for(auto &v : s) {
		view(v);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <class T>
void view(const T &e) { std::cerr << e; }
}  // namespace debugger
#ifdef LOCAL
void debug_out() {}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	debugger::view(H);
	std::cerr << ", ";
	debug_out(T...);
}
#define debug(...)                                                \
	do {                                                          \
		std::cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \
		debug_out(__VA_ARGS__);                                   \
		std::cerr << "\b\b]\n";                                   \
	} while(false)
#else
#define debug(...) (void(0))
#endif

// vector macro
template <class T>
int lb(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::lower_bound((a).begin(), (a).end(), (x))); }
template <class T>
int ub(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::upper_bound((a).begin(), (a).end(), (x))); }
template <class T>
void UNIQUE(std::vector<T> &a) {
	std::sort(a.begin(), a.end());
	a.erase(std::unique(a.begin(), a.end()), a.end());
}
template <class T>
std::vector<T> press(std::vector<T> &a) {
	auto res = a;
	UNIQUE(res);
	for(auto &v : a)
		v = lb(res, v);
	return res;
}
#define SORTname(a, b, c, ...) c
#define SORT(...) SORTname(__VA_ARGS__, SORT1, SORT0, ...)(__VA_ARGS__)
#define SORT0(a) std::sort((a).begin(), (a).end())
#define SORT1(a, c) std::sort((a).begin(), (a).end(), [](const auto x, const auto y) { return x c y; })
template <class T>
void ADD(std::vector<T> &a, const T x = 1) {
	for(auto &v : a) v += x;
}
template <class T>
void SUB(std::vector<T> &a, const T x = 1) {
	for(auto &v : a) v -= x;
}
template <class T>
void MUL(std::vector<T> &a, const T x) {
	for(auto &v : a) v *= x;
}
template <class T>
void DIV(std::vector<T> &a, const T x) {
	for(auto &v : a) v /= x;
}
std::vector<std::pair<char, int>> rle(const string &s) {
	int n = s.size();
	std::vector<std::pair<char, int>> ret;
	for(int l = 0; l < n;) {
		int r = l + 1;
		for(; r < n and s[l] == s[r]; r++) {}
		ret.emplace_back(s[l], r - l);
		l = r;
	}
	return ret;
}
template <class T>
std::vector<std::pair<T, int>> rle(const std::vector<T> &v) {
	int n = v.size();
	std::vector<std::pair<T, int>> ret;
	for(int l = 0; l < n;) {
		int r = l + 1;
		for(; r < n and v[l] == v[r]; r++) {}
		ret.emplace_back(v[l], r - l);
		l = r;
	}
	return ret;
}

// math macro
template <class T, class U>
inline bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; }
template <class T, class U>
inline bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; }
template <class T>
T divup(T x, T y) { return (x + y - 1) / y; }
template <class T>
T POW(T a, long long n) {
	T ret = 1;
	while(n) {
		if(n & 1) ret *= a;
		a *= a;
		n >>= 1;
	}
	return ret;
}
// modpow
long long POW(long long a, long long n, const int mod) {
	long long ret = 1;
	a = (a % mod + mod) % mod;
	while(n) {
		if(n & 1) (ret *= a) %= mod;
		(a *= a) %= mod;
		n >>= 1;
	}
	return ret;
}

// others
struct fast_io {
	fast_io() {
		ios::sync_with_stdio(false);
		cin.tie(nullptr);
		cout << fixed << setprecision(15);
	}
} fast_io_;
const int inf = 1e9;
const ll INF = 1e18;
#pragma endregion

void main_();

int main() {
	main_();
	return 0;
}
#line 2 "a.cpp"

const int N = 20000;
void main_() {
	while(1) {
		STR(s);
		if(s == "#") break;
		int n = SZ(s);
		vector dp(n + 1, vector(2, vector(100, inf)));
		//左 * 9 + 右
		V<> valid(100);
		REPS(i, 9) {
			REPS(j, 9) {
				if(i == 5 or j == 5) continue;
				if(i == 2 or i == 8) {
					if(j == 1 or j == 4 or j == 7) {
						continue;
					}
				}
				if(i == 3 or i == 6 or i == 9) {
					if(j == 1 or j == 4 or j == 7) {
						continue;
					}
					if(j == 2 or j == 8) {
						continue;
					}
				}
				valid[i * 9 + j] = 1;
			}
		}
		REP(i, 2) {
			REP(j, 100) {
				if(valid[j]) dp[0][i][j] = 0;
			}
		}
		REP(i, n) {
			REP(oj, 2) {
				REP(ov, 100) {
					if(dp[i][oj][ov] == inf) continue;
					int rig = ov % 9;
					if(rig == 0) rig = 9;
					int lef = (ov - rig) / 9;
					REP(nj, 2) {
						int nx = s[i] - '0';
						if(nj == 0) {
							int nxt = nx * 9 + rig;
							if(!valid[nxt]) continue;
							chmin(dp[i + 1][nj][nxt], dp[i][oj][ov] + (oj == nj));
						}
						if(nj == 1) {
							int nxt = lef * 9 + nx;
							if(!valid[nxt]) continue;
							chmin(dp[i + 1][nj][nxt], dp[i][oj][ov] + (oj == nj));
						}
					}
				}
			}
		}
		int res = inf;
		REP(i, 2) {
			REP(j, 100) {
				chmin(res, dp.back()[i][j]);
			}
		}
		print(res);
	}
}
accuracy: 1
time_ms: 370
memory_kb: 93012
final_rank: 18

code_snippet: #ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
 
#include <bits/stdc++.h>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
 
#pragma region Macros
// rep macro
#define foa(v, a) for(auto &v : a)
#define REPname(a, b, c, d, e, ...) e
#define REP(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)
#define REP0(x) for(int i = 0; i < (x); ++i)
#define REP1(i, x) for(int i = 0; i < (x); ++i)
#define REP2(i, l, r) for(int i = (l); i < (r); ++i)
#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))
#define REPSname(a, b, c, ...) c
#define REPS(...) REPSname(__VA_ARGS__, REPS1, REPS0)(__VA_ARGS__)
#define REPS0(x) for(int i = 1; i <= (x); ++i)
#define REPS1(i, x) for(int i = 1; i <= (x); ++i)
#define RREPname(a, b, c, d, e, ...) e
#define RREP(...) RREPname(__VA_ARGS__, RREP3, RREP2, RREP1, RREP0)(__VA_ARGS__)
#define RREP0(x) for(int i = (x)-1; i >= 0; --i)
#define RREP1(i, x) for(int i = (x)-1; i >= 0; --i)
#define RREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)
#define RREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))
#define RREPSname(a, b, c, ...) c
#define RREPS(...) RREPSname(__VA_ARGS__, RREPS1, RREPS0)(__VA_ARGS__)
#define RREPS0(x) for(int i = (x); i >= 1; --i)
#define RREPS1(i, x) for(int i = (x); i >= 1; --i)
 
// name macro
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define popcnt(x) __builtin_popcountll(x)
template <class T = int>
using V = std::vector<T>;
template <class T = int>
using VV = std::vector<std::vector<T>>;
template <class T = int>
using VVV = std::vector<std::vector<std::vector<T>>>;
template <class T>
using pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;
using ll = long long;
using ld = long double;
using int128 = __int128_t;
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;
using Tp = tuple<ll,ll,ll>;
 
// input macro
template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
	is >> p.first >> p.second;
	return is;
}
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
	for(T &i : v) is >> i;
	return is;
}
std::istream &operator>>(std::istream &is, __int128_t &a) {
	std::string s;
	is >> s;
	__int128_t ret = 0;
	for(int i = 0; i < s.length(); i++)
		if('0' <= s[i] and s[i] <= '9')
			ret = 10 * ret + s[i] - '0';
	a = ret * (s[0] == '-' ? -1 : 1);
	return is;
}
namespace scanner {
void scan(int &a) { std::cin >> a; }
void scan(long long &a) { std::cin >> a; }
void scan(std::string &a) { std::cin >> a; }
void scan(char &a) { std::cin >> a; }
void scan(char a[]) { std::scanf("%s", a); }
void scan(double &a) { std::cin >> a; }
void scan(long double &a) { std::cin >> a; }
template <class T, class U>
void scan(std::pair<T, U> &p) { std::cin >> p; }
template <class T>
void scan(std::vector<T> &a) { std::cin >> a; }
void INPUT() {}
template <class Head, class... Tail>
void INPUT(Head &head, Tail &... tail) {
	scan(head);
	INPUT(tail...);
}
}  // namespace scanner
#define VEC(type, name, size)     \
	std::vector<type> name(size); \
	scanner::INPUT(name)
#define VVEC(type, name, h, w)                                    \
	std::vector<std::vector<type>> name(h, std::vector<type>(w)); \
	scanner::INPUT(name)
#define INT(...)     \
	int __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LL(...)            \
	long long __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define STR(...)             \
	std::string __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define CHAR(...)     \
	char __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define DOUBLE(...)     \
	double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LD(...)              \
	long double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
 
// output-macro
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
	os << p.first << " " << p.second;
	return os;
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &a) {
	for(int i = 0; i < int(a.size()); ++i) {
		if(i) os << " ";
		os << a[i];
	}
	return os;
}
std::ostream &operator<<(std::ostream &dest, __int128_t &value) {
	std::ostream::sentry s(dest);
	if(s) {
		__uint128_t tmp = value < 0 ? -value : value;
		char buffer[128];
		char *d = std::end(buffer);
		do {
			--d;
			*d = "0123456789"[tmp % 10];
			tmp /= 10;
		} while(tmp != 0);
		if(value < 0) {
			--d;
			*d = '-';
		}
		int len = std::end(buffer) - d;
		if(dest.rdbuf()->sputn(d, len) != len) {
			dest.setstate(std::ios_base::badbit);
		}
	}
	return dest;
}
template <class T>
void print(const T a) { std::cout << a << '\n'; }
template <class Head, class... Tail>
void print(Head H, Tail... T) {
	std::cout << H << ' ';
	print(T...);
}
template <class T>
void printel(const T a) { std::cout << a << '\n'; }
template <class T>
void printel(const std::vector<T> &a) {
	for(const auto &v : a)
		std::cout << v << '\n';
}
template <class Head, class... Tail>
void printel(Head H, Tail... T) {
	std::cout << H << '\n';
	printel(T...);
}
void Yes(const bool b = true) { std::cout << (b ? "Yes\n" : "No\n"); }
void No() { std::cout << "No\n"; }
void YES(const bool b = true) { std::cout << (b ? "YES\n" : "NO\n"); }
void NO() { std::cout << "No\n"; }
void err(const bool b = true) {
	if(b) {
		std::cout << "-1\n", exit(0);
	}
}
 
//debug macro
namespace debugger {
template <class T>
void view(const std::vector<T> &a) {
	std::cerr << "{ ";
	for(const auto &v : a) {
		std::cerr << v << ", ";
	}
	std::cerr << "\b\b }";
}
template <class T>
void view(const std::vector<std::vector<T>> &a) {
	std::cerr << "{\n";
	for(const auto &v : a) {
		std::cerr << "\t";
		view(v);
		std::cerr << "\n";
	}
	std::cerr << "}";
}
template <class T, class U>
void view(const std::vector<std::pair<T, U>> &a) {
	std::cerr << "{\n";
	for(const auto &p : a) std::cerr << "\t(" << p.first << ", " << p.second << ")\n";
	std::cerr << "}";
}
template <class T, class U>
void view(const std::map<T, U> &m) {
	std::cerr << "{\n";
	for(const auto &p : m) std::cerr << "\t[" << p.first << "] : " << p.second << "\n";
	std::cerr << "}";
}
template <class T, class U>
void view(const std::pair<T, U> &p) { std::cerr << "(" << p.first << ", " << p.second << ")"; }
template <class T>
void view(const std::set<T> &s) {
	std::cerr << "{ ";
	for(auto &v : s) {
		view(v);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}
 
template <class T>
void view(const T &e) { std::cerr << e; }
}  // namespace debugger
#ifdef LOCAL
void debug_out() {}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	debugger::view(H);
	std::cerr << ", ";
	debug_out(T...);
}
#define debug(...)                                                \
	do {                                                          \
		std::cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \
		debug_out(__VA_ARGS__);                                   \
		std::cerr << "\b\b]\n";                                   \
	} while(false)
#else
#define debug(...) (void(0))
#endif
 
// vector macro
template <class T>
int lb(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::lower_bound((a).begin(), (a).end(), (x))); }
template <class T>
int ub(const std::vector<T> &a, const T x) { return std::distance((a).begin(), std::upper_bound((a).begin(), (a).end(), (x))); }
template <class T>
void UNIQUE(std::vector<T> &a) {
	std::sort(a.begin(), a.end());
	a.erase(std::unique(a.begin(), a.end()), a.end());
}
template <class T>
std::vector<T> press(std::vector<T> &a) {
	auto res = a;
	UNIQUE(res);
	for(auto &v : a)
		v = lb(res, v);
	return res;
}
#define SORTname(a, b, c, ...) c
#define SORT(...) SORTname(__VA_ARGS__, SORT1, SORT0, ...)(__VA_ARGS__)
#define SORT0(a) std::sort((a).begin(), (a).end())
#define SORT1(a, c) std::sort((a).begin(), (a).end(), [](const auto x, const auto y) { return x c y; })
template <class T>
void ADD(std::vector<T> &a, const T x) {
	for(auto &v : a) v += x;
}
template <class T>
void SUB(std::vector<T> &a, const T x = 1) {
	for(auto &v : a) v -= x;
}
template <class T>
void MUL(std::vector<T> &a, const T x) {
	for(auto &v : a) v *= x;
}
template <class T>
void DIV(std::vector<T> &a, const T x) {
	for(auto &v : a) v /= x;
}
 
// math macro
template <class T, class U>
inline bool chmin(T &a, const U &b) { return a > b ? a = b, true : false; }
template <class T, class U>
inline bool chmax(T &a, const U &b) { return a < b ? a = b, true : false; }
template <class T>
T divup(T x, T y) { return (x + y - 1) / y; }
template <class T>
T POW(T a, long long n) {
	T ret = 1;
	while(n) {
		if(n & 1) ret *= a;
		a *= a;
		n >>= 1;
	}
	return ret;
}

template<typename T>
T ADD(T a, T b){
	T res;
	return __builtin_add_overflow(a, b, &res) ? numeric_limits<T>::max() : res;
}

template<typename T>
T MUL(T a, T b){
	T res;
	return __builtin_mul_overflow(a, b, &res) ? numeric_limits<T>::max() : res;
}

// modpow
long long POW(long long a, long long n, const int mod) {
	long long ret = 1;
	while(n) {
		if(n & 1) (ret *= a) %= mod;
		(a *= a) %= mod;
		n >>= 1;
	}
	return ret;
}
 
// others
struct fast_io {
	fast_io() {
		ios::sync_with_stdio(false);
		cin.tie(nullptr);
		cout << fixed << setprecision(15);
	}
} fast_io_;

#pragma endregion

using R = long double;
constexpr R EPS=1E-11;

// r の(誤差付きの)符号に従って, -1, 0, 1 を返す.
int sgn(const R& r){ return (r > EPS) - (r < -EPS); }
// a, b の(誤差付きの)大小比較の結果に従って, -1, 0, 1 を返す.
int sgn(const R& a, const R &b){ return sgn(a-b); }
// a > 0 は sgn(a) > 0
// a < b は sgn(a, b) < 0
// a >= b は sgn(a, b) >= 0
// のように書く.
// return s * 10^n

//https://atcoder.jp/contests/abc191/submissions/20028529
long long x10(const string& s, size_t n) {
    if (s.front() == '-') return -x10(s.substr(1), n);
    auto pos = s.find('.');
    if (pos == string::npos) return stoll(s + string(n, '0'));
    return stoll(s.substr(0, pos) + s.substr(pos + 1) + string(n + pos + 1 - s.size(), '0'));
}
 
long long ceildiv(long long a, long long b) {
    if (b < 0) a = -a, b = -b;
    if (a >= 0) return (a + b - 1) / b;
    else return a / b;
}
 
long long floordiv(long long a, long long b) {
    if (b < 0) a = -a, b = -b;
    if (a >= 0) return a / b;
    else return (a - b + 1) / b;
}
 
long long floorsqrt(long long x) {
    assert(x >= 0);
    long long ok = 0;
    long long ng = 1;
    while (ng * ng <= x) ng <<= 1;
    while (ng - ok > 1) {
        long long mid = (ng + ok) >> 1;
        if (mid * mid <= x) ok = mid;
        else ng = mid;
    }
    return ok;
}

inline int topbit(unsigned long long x){
	return x?63-__builtin_clzll(x):-1;
}

inline int popcount(unsigned long long x){
	return __builtin_popcountll(x);
}

inline int parity(unsigned long long x){//popcount%2
	return __builtin_parity(x);
}

const int inf = 1e9;
const ll INF = 1e18;

int dx[9] = {-1,0,1,-1,0,1,-1,0,1};
int dy[9] = {-1,-1,-1,0,0,0,1,1,1};

string s;


void main_() {
	
	int n = s.length();
	VVV<int> dp(n+1,VV<int>(9,V<int>(2,inf)));
	REP(i,9){
	    REP(j,2){
	        dp[0][i][j] = 0;
	    }
	}
	
	REP(i,n){
	    int num = s[i] - '1';
	    REP(j,9){
	        REP(p,2){
	            REP(q,2){
	                if(p == 0 && q == 1 && dx[num] < dx[j])continue;
	                if(p == 1 && q == 0 && dx[num] > dx[j])continue;
	                dp[i+1][num][q] = min(dp[i+1][num][q],dp[i][j][p] + (p == q));
	            }
	        }
	    }
	}
	int ans = inf;
	REP(i,9){
	    REP(j,2){
	        ans = min(ans,dp[n][i][j]);
	    }
	}
	cout << ans << endl;
}
 
int main() {
	int t = 1;
	//cin >> t;
	cin >> s;
	while(s != "#"){
	    main_();
	    cin >> s;
	}
	return 0;
}
accuracy: 1
time_ms: 290
memory_kb: 55376
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
    string S;
    const int INF = 1e7;
    while (cin >> S, S != "#") {
        vector maine(3, vector(3, vector(2, 0)));
        for (auto&& c : S) {
            (c -= '1') %= 3;
            vector book(3, vector(3, vector(2, INF)));
            for (int l = 0; l < 3; l++) {
                for (int r = l; r < 3; r++) {
                    if (c <= r)
                        book[c][r][1] = min({book[c][r][1], maine[l][r][0], maine[l][r][1] + 1});
                    if (c >= l)
                        book[l][c][0] = min({book[l][c][0], maine[l][r][1], maine[l][r][0] + 1});
                }
            }
            swap(maine, book);
        }
        int ans = INF;
        for (int i = 0; i < 3; i++) {
            for (int j = i; j < 3; j++) {
                for (int k = 0; k < 2; k++) {
                    ans = min(ans, maine[i][j][k]);
                }
            }
        }
        cout << ans << endl;
    }
}
accuracy: 1
time_ms: 150
memory_kb: 3372
final_rank: 6

code_snippet: #include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <tuple>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <functional>
#include <numeric>
#include <iomanip> 
#include <stdio.h>
//eolibraries
#define lnf 3999999999999999999
#define inf 999999999
#define PI 3.14159265359
#define endl "\n"
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define ll long long
#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()
#define mkp(a,b) make_pair(a,b)
#define make_unique(a) sort(all(a)),a.erase(unique(all(a)),a.end())
#define rsz(a,n) a.resize(n)
#define pii pair <int,int>
#define rep(i,n) for(int i = 0 ; i < n ; i++) 
#define drep(i,n) for(int i = n-1 ; i >= 0 ; i--)
#define crep(i,x,n) for(int i = x ; i < n ; i++)
#define vi vector <int> 
#define vec(...) vector<__VA_ARGS__>
#define rsz(a,n) a.resize(n)
#define rszv(a,n,v) a.resize(n,v)
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define chmin(a,b) a=min(a,b)
//eodefine

using namespace std;

const int max_n = 100002;

int dp[max_n][11][11][2];

void slv(string s) {
	int n=sz(s);
	vi a;
	rep(i,n) {
		a.pb(s[i]-'0');
	}

	rep(i,n+1) {
		rep(lval,10) {
			rep(rval,10) {
				rep(step,2) dp[i][lval][rval][step]=inf;
			}
		}
	}

	dp[0][5][5][0]=0;
	dp[0][5][5][1]=0;

	function<bool(int,int)> valid = [&](int lval,int rval) {
		if(lval==1 or lval==4 or lval==7) return true;
		if(lval==2 or lval==8) {
			if(rval==1 or rval==4 or rval==7) return false;
			return true;
		}
		if(rval!=9 and rval!=6 and rval!=3) return false;
		return  true;
	};

	rep(i,n) {
		int val=a[i];
		rep(lval,10) {
			rep(rval,10) {
				rep(step,2) {
					if(valid(val,rval) or rval==5){
						chmin(dp[i+1][val][rval][0] , dp[i][lval][rval][step] + (step==0));
					}
					if(valid(lval,val) or lval==5){
						chmin(dp[i+1][lval][val][1] , dp[i][lval][rval][step] + (step==1));
					}
				}
			}
		}
	}

	int ans=inf;
	
	rep(lval,10) {
		rep(rval,10) {
			rep(step,2) {
				chmin(ans,dp[n][lval][rval][step]);
			}
		}
	} 
	
	cout<<ans<<"\n";
}

int main(){
fcin;

	while(true) {
		string s;
		cin>>s;
		if(s=="#") break;
		slv(s);
	}
	
/*

*/
 
    return 0;   
}
accuracy: 1
time_ms: 840
memory_kb: 98652
final_rank: 19

code_snippet: #include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <tuple>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <functional>
#include <numeric>
#include <iomanip> 
#include <stdio.h>
//eolibraries
#define lnf 3999999999999999999
#define inf 999999999
#define PI 3.14159265359
#define endl "\n"
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define ll long long
#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()
#define mkp(a,b) make_pair(a,b)
#define make_unique(a) sort(all(a)),a.erase(unique(all(a)),a.end())
#define rsz(a,n) a.resize(n)
#define pii pair <int,int>
#define rep(i,n) for(int i = 0 ; i < n ; i++) 
#define drep(i,n) for(int i = n-1 ; i >= 0 ; i--)
#define crep(i,x,n) for(int i = x ; i < n ; i++)
#define vi vector <int> 
#define vec(...) vector<__VA_ARGS__>
#define rsz(a,n) a.resize(n)
#define rszv(a,n,v) a.resize(n,v)
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define chmin(a,b) a=min(a,b)
//eodefine

using namespace std;

const int max_n = 100002;

int dp[max_n][10][10][3];

void slv(string s) {
	int n=sz(s);
	vi a;
	rep(i,n) {
		a.pb(s[i]-'0');
	}

	rep(i,n+1) {
		rep(lval,10) {
			rep(rval,10) {
				rep(step,3) dp[i][lval][rval][step]=inf;
			}
		}
	}

	dp[0][5][5][0]=0;
	dp[0][5][5][1]=0;

	function<bool(int,int)> valid = [&](int lval,int rval) {
		if(lval==1 or lval==4 or lval==7) return true;
		if(lval==2 or lval==8) {
			if(rval==1 or rval==4 or rval==7) return false;
			return true;
		}
		if(rval!=9 and rval!=6 and rval!=3) return false;
		return  true;
	};

	rep(i,n) {
		int val=a[i];
		rep(lval,10) {
			rep(rval,10) {
				rep(step,3) {
					if(valid(val,rval) or rval==5){
						chmin(dp[i+1][val][rval][0] , dp[i][lval][rval][step] + (step==0));
					}
					if(valid(lval,val) or lval==5){
						chmin(dp[i+1][lval][val][1] , dp[i][lval][rval][step] + (step==1));
					}
				}
			}
		}
	}

	int ans=inf;
	
	rep(lval,10) {
		rep(rval,10) {
			rep(step,3) {
				chmin(ans,dp[n][lval][rval][step]);
			}
		}
	} 
	
	cout<<ans<<"\n";
}

int main(){
fcin;

	while(true) {
		string s;
		cin>>s;
		if(s=="#") break;
		slv(s);
	}
	
/*

*/
 
    return 0;   
}
accuracy: 1
time_ms: 1210
memory_kb: 121292
final_rank: 20

code_snippet: #include <bits/stdc++.h>

#pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
//#include <boost/multiprecision/cpp_int.hpp>
using namespace std;

//using namespace boost::multiprecision;
//#include<atcoder/all>
//#include<atcoder/segtree>
//#include <atcoder/scc>
//using namespace atcoder;

using dou =long double;
string yes="yes";
string Yes="Yes";
string YES="YES";
string no="no";
string No="No";
string NO="NO";

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

ll mod = 998244353ll;
//ll mod = 1000000007ll;
//const ll mod = 4;


struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
  mint operator+(const mint a) const { return mint(*this) += a;}
  mint operator-(const mint a) const { return mint(*this) -= a;}
  mint operator*(const mint a) const { return mint(*this) *= a;}
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  // for prime modhttps://atcoder.jp/contests/abc166/submit?taskScreenName=abc166_f
  mint inv() const { return pow(mod-2);}
  mint& operator/=(const mint a) { return *this *= a.inv();}
  mint operator/(const mint a) const { return mint(*this) /= a;}
};

istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

#define rep(i, n)         for(ll i = 0; i < (ll)(n); i++)
//#define rep(i, n)         for(int i = 0; i < (int)(n); i++)
#define brep(n)           for(int bit=0;bit<(1<<n);bit++)
#define bbrep(n)           for(int bbit=0;bbit<(1<<n);bbit++)
#define erep(i,container) for (auto &i : container)
#define itrep(i,container) for (auto i : container)
#define irep(i, n)        for(ll i = n-1; i >= (ll)0ll; i--)
#define rrep(i,m,n) for(ll i = m; i < (ll)(n); i++)
#define reprep(i,j,h,w) rep(i,h)rep(j,w)
#define repreprep(i,j,k,h,w,n) rep(i,h)rep(j,w)rep(k,n)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define VEC(type,name,n) std::vector<type> name(n);rep(i,n)std::cin >> name[i];
#define pb push_back
#define pf push_front
#define query int qq;std::cin >> qq;rep(qqq,qq)
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define itn int
#define mp make_pair
//#define sum(a) accumulate(all(a),0ll)
#define keta fixed<<setprecision
#define kout(d) std::cout << keta(10)<< d<< std::endl;
#define vout(a) erep(qxqxqx,a)std::cout << qxqxqx << ' ';std::cout  << std::endl;
#define vvector(name,typ,m,n,a)vector<vector<typ> > name(m,vector<typ> (n,a))
//#define vvector(name,typ,m,n)vector<vector<typ> > name(m,vector<typ> (n))
#define vvvector(name,t,l,m,n,a) vector<vector<vector<t> > > name(l, vector<vector<t> >(m, vector<t>(n,a)));
#define vvvvector(name,t,k,l,m,n,a) vector<vector<vector<vector<t> > > > name(k,vector<vector<vector<t> > >(l, vector<vector<t> >(m, vector<t>(n,a)) ));
//#define case std::cout <<"Case #" <<qqq+1<<":"
#define RES(a,iq,jq) a.resize(iq);rep(iii,iq)a[iii].resize(jq);

#define RESRES(a,i,j,k) a.resize(i);rep(ii,i)a[ii].resize(j);reprep(ii,jj,i,j){a[ii][jj].resize(k);};
#define res resize
#define as assign
#define ffor for(;;)
#define ppri(a,b) std::cout << a<<" "<<b << std::endl
#define pppri(a,b,c) std::cout << a<<" "<<b <<" "<< c<<std::endl
#define ppppri(a,b,c,d) std::cout << a<<" "<<b <<" "<< c<<' '<<d<<std::endl
#define aall(x,n) (x).begin(),(x).begin()+(n)
#define SUMI(a) accumulate(all(a),0) 
#define SUM(a) accumulate(all(a),0ll) 
#define stirng string
#define gin(a,b) int a,b;std::cin >> a>>b;a--;b--;
#define popcount __builtin_popcount
#define permu(a) next_permutation(all(a))
#define aru(a,d) a.find(d)!=a.end()
#define nai(a,d) a.find(d)==a.end()
//#define aru p.find(mp(x,y))!=p.end()
//#define grid_input(a,type) int h,w;std::cin >> h>>w;vvector(a,type,h,w,0);reprep(i,j,h,w)std::cin >> a[i][j];

//typedef long long T;
ll ceili(ll a,ll b){
    return ((a+b-1)/b);
}
const int INF = 2000000000;
//const ll INF64 =922330720854775807ll;
const ll INF64 = 4223372036854775807ll;
//const ll INF64 = 9223372036854775807ll;

//const ll INF64 = 243'000'000'000'000'000'0;Q


//const ll MOD = 1000000007ll;
const ll MOD = 998244353ll;
//const ll MOD = 1000003ll;
const ll OD = 1000000000000007ll;
const dou pi=3.141592653589793;

long long modpow(long long a, long long n) { 
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % MOD;
        a = a * a % MOD;
        n >>= 1;
    }
    return res;
}
vector< ll > divisor(ll n) {  //約数の列挙
  vector< ll > ret;
  for(ll i = 1; i * i <= n; i++) {
    if(n % i == 0) {
      ret.push_back(i);
      if(i * i != n) ret.push_back(n / i);
    }
  }
  sort(begin(ret), end(ret));
  return (ret);
}
//メモ
//ゲーム(Grundy数とか)の復習をする
//リスニング力をどうにかする
//個数制限付きナップサックの復習
//戻すDP
//全方位木DPとスライド最小値
//ゲーム→パリティに注目するといいことあるかも
//ゲーム→もとの状態に戻せる状態を探す
//理由がなければLLを使え！
//理由がなければLLを使え！
//理由がなければLLを使え！
//理由がなければLLを使え！
//理由がなければLLを使え！
//理由がなければLLを使え！
//理由がなければLLを使え！
//理由がなければLLを使え！
//来週の月曜日に銀行へ行く
//積率を調べる実験を来週の火曜日までにやる
//レピュニット数
//フェルマーの小定理
//拡張ユークリッド

struct edge {// ダイクストラ法
    ll to, cost;
    
    edge(ll to,ll cost):
     to(to),cost(cost){};
     

};
typedef vector<vector<edge> > AdjList;  // 隣接リストの型
typedef vector<vector<edge> > Graph;
vector<ll> dist;
typedef pair<int, int> P;
void dijkstra(AdjList& graph,int n, int s){
    dist.clear();
    dist = vector<ll>(n,INF64);
    dist[s] = 0;
    priority_queue<P, vector<P>, greater<P> > que;
    que.push(P(0,s));
    while(!que.empty()){
        P p = que.top();
        que.pop();
        int v = p.second;
        if(dist[v] < p.first){
            continue;
        }
        for(int i=0;i < graph[v].size();i++){
            edge e = graph[v][i];
            if(dist[e.to] > dist[v] + e.cost){
                dist[e.to] = dist[v] + e.cost;
                que.push(P(dist[e.to],e.to));
            }
        }
    }
}


int main(){
    ffor{
        string s;std::cin >> s;
        if(s=="#")break;
        int n=s.size();
        std::vector<std::vector<edge>> g(n*2+2);
        int st=n*2,go=n*2+1;
        g[st].pb(edge(0,0));
        g[st].pb(edge(1,0));
        g[n*2-2].pb(edge(go,0));
        g[n*2-1].pb(edge(go,0));
        std::vector<int> v{-1,0,1,-1,0,1,-1,0,1};
        rep(i,n-1){
            int be=v[s[i]-'1'];
            int af=v[s[i+1]-'1'];
            if(be>af){
                g[i*2].pb(edge( (i+1)*2,1 ));
                //g[i*2].pb(edge( (i+1)*2+1,0 ));
                g[i*2+1].pb(edge( (i+1)*2+1,1 ));
                g[i*2+1].pb(edge( (i+1)*2,0 ));
            }
            else if(be<af){
                g[i*2].pb(edge( (i+1)*2,1 ));
                g[i*2].pb(edge( (i+1)*2+1,0 ));
                g[i*2+1].pb(edge( (i+1)*2+1,1 ));
                //g[i*2+1].pb(edge( (i+1)*2,0 ));
            }
            else{
                g[i*2].pb(edge( (i+1)*2,1 ));
                g[i*2].pb(edge( (i+1)*2+1,0 ));
                g[i*2+1].pb(edge( (i+1)*2+1,1 ));
                g[i*2+1].pb(edge( (i+1)*2,0 ));
            }
        }
        dijkstra(g,n*2+2,st);
        std::cout << dist[go] << std::endl;
        
    }
}
accuracy: 1
time_ms: 110
memory_kb: 19792
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;

int dp[2][10][10][3];
bool is_allowed(int left, int right) {
    left = left % 3 == 0 ? 3 : left % 3;
    right = right % 3 == 0 ? 3 : right % 3;
    return left <= right;
}
int main() {
    while (1) {
        string notes;
        cin >> notes;
        if (notes == "#")
            break;
        int n = notes.size();
        fill((int *)dp, (int *)(dp + 2), 1 << 30);
        vector<int> moves = {1, 2, 3, 4, 6, 7, 8, 9};
        for (int l : moves) {
            for (int r : moves) {
                if (!is_allowed(l, r))
                    continue;
                dp[0][l][r][0] = 0;
            }
        }
        for (int i = 0; i < n; i++) {
            int now = i & 1;
            int nxt = now ^ 1;
            for (int l : moves) {
                for (int r : moves) {
                    for (int last = 0; last < 3; last++) {
                        if (dp[now][l][r][last] == 1 << 30)
                            continue;
                        //左足で押す
                        if (is_allowed(notes[i] - '0', r)) {
                            dp[nxt][notes[i] - '0'][r][1] =
                                min(dp[nxt][notes[i] - '0'][r][1],
                                    dp[now][l][r][last] + (last == 1));
                        }
                        //右足で押す
                        if (is_allowed(l, notes[i] - '0')) {
                            dp[nxt][l][notes[i] - '0'][2] =
                                min(dp[nxt][l][notes[i] - '0'][2],
                                    dp[now][l][r][last] + (last == 2));
                        }
                    }
                }
            }
            for (int l : moves)
                for (int r : moves)
                    for (int last = 0; last < 3; last++)
                        dp[now][l][r][last] = 1 << 30;
        }
        int ans = 1 << 30;
        for (int l : moves) {
            for (int r : moves) {
                ans = min(ans, dp[n & 1][l][r][1]);
                ans = min(ans, dp[n & 1][l][r][2]);
            }
        }
        cout << ans << '\n';
    }
}
accuracy: 1
time_ms: 180
memory_kb: 3372
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define int long long

// debug methods
// usage: debug(x,y);
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0,a1,a2,a3,a4,x,...) x
#define debug_1(x1) cout<<#x1<<": "<<x1<<endl
#define debug_2(x1,x2) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<endl
#define debug_3(x1,x2,x3) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<endl
#define debug_4(x1,x2,x3,x4) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<endl
#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<endl
#ifdef _DEBUG
#define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)
#else
#define debug(...)
#endif

#define MAX_N (1000006)
#define INF (1LL << 60)
#define eps 1e-9
const int MOD = (int)1e9 + 7;


signed main(){
    //cin.tie(nullptr);
    //std::ios::sync_with_stdio(false);

    while(1){
        string s;
        cin >> s;
        if(s=="#") break;

        debug(s);
        int v[] = {-1,0,1,2,0,1,2,0,1,2};

        int ans = INF;
        bool is_left = false;
        // 左足、右足の順でスタートする
        for(int l=0; l<2; l++){
            debug(l);
            int left = 10;
            int right = 10;
            if(is_left) right = v[s[0]-'0'];
            else left = v[s[0]-'0'];

            int cnt = 0;
            for(int i=1; i<s.length(); i++){
                int c = s[i]-'0';
                debug(i,s[i],v[c],cnt);
                if(is_left){
                    // 左足を c に動かす
                    // 右足より左なら cnt++ して、右足を動かす
                    if(v[c] > right) {
                        cnt++;
                        right = v[c];
                    } else {
                        left = v[c];
                        is_left = false;
                    }
                } else {
                    if(v[c] < left) {
                        cnt++;
                        left = v[c];
                    } else {
                        right = v[c];
                        is_left = true;
                    }
                }
                debug(cnt);
            }
            ans = min(ans, cnt);
            is_left = true;
        }
        cout << ans << endl;
    }
}

/*


*/
accuracy: 1
time_ms: 10
memory_kb: 3184
final_rank: 1