#include <bits/stdc++.h>
 
using namespace std;
#define all(x) (x).begin(),(x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define endl "\n"
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {os << "["; for (const auto &v : vec) {os << v << ","; } os << "]"; return os;}
template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) {os << "(" << p.first << ", " << p.second << ")"; return os;}
 
void solve() {
    string S;
    cin >> S;
    int N = S.size();
    const int INF = 1 << 29;
    vector<vector<int>> dp(N + 1, vector<int>(3, INF));
    dp[0][0] = 0;
    for(int i = 0; i < N; i++) {
        for(int j = 0; j < 3; j++) {
            int nj = (j + S[i] - '0') % 3;
            dp[i + 1][nj] = min(dp[i + 1][nj], dp[i][j]);
            dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + 1);
        }
    }
    if (dp[N][0] == N) {
        cout << -1 << endl;
    } else {
        cout << dp[N][0] << endl;
    }
}

int main() {
    #ifdef LOCAL_ENV
    cin.exceptions(ios::failbit);
    #endif
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout.setf(ios::fixed);
    cout.precision(16);
    
    solve();
}
#include "bits/stdc++.h"
#define int long long
#define endl '\n'
using namespace std;
 
typedef long long ll;
typedef long double ld;
#define db(x) cerr << #x << ": " << x << '\n';
#define read(a) int a; cin >> a;
#define reads(s) string s; cin >> s;
#define readb(a, b) int a, b; cin >> a >> b;
#define readc(a, b, c) int a, b, c; cin >> a >> b >> c;
#define readarr(a, n) int a[(n) + 1] = {}; FOR(i, 1, (n)) {cin >> a[i];}
#define readmat(a, n, m) int a[n + 1][m + 1] = {}; FOR(i, 1, n) {FOR(j, 1, m) cin >> a[i][j];}

#define print(a) cout << a << endl;
#define printarr(a, n) FOR (i, 1, n) cout << a[i] << " "; cout << endl;
#define printv(v) for (int i: v) cout << i << " "; cout << endl;
#define printmat(a, n, m) FOR (i, 1, n) {FOR (j, 1, m) cout << a[i][j] << " "; cout << endl;}
#define all(v) v.begin(), v.end()
#define sz(v) (int)(v.size())
#define rz(v, n) v.resize((n) + 1);
#define pb push_back
#define fi first
#define se second
#define vi vector <int>
#define pi pair <int, int>
#define vpi vector <pi>
#define vvi vector <vi>
#define setprec cout << fixed << showpoint << setprecision(20);
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const ll inf = 1e18;
const ll mod = 998244353;
const ll N = 2e5 + 1;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int power (int a, int b = mod - 2)
{
    int res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}

signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	readb(n, m);
	int one[n + 1][m + 1] = {}, zer[n + 1][m + 1] = {};
	int onesum[n + 1][m + 1] = {}, zersum[n + 1][m + 1] = {};
	
	int precalc[m + 1][n + 1] = {};
	FOR (i, 1, m) precalc[i][0] = 1;
	FOR (i, 1, m)
	    FOR (j, 1, n)
	        precalc[i][j] = i*precalc[i][j - 1] % mod;
	        
	FOR (i, 1, n)
	    FOR (j, 1, m)
	    {
	        one[i][j] = (one[i][j - 1] + onesum[i - 1][j - 1] + zersum[i - 1][j - 1] + precalc[j][i] - precalc[j - 1][i]) % mod;
	        zer[i][j] = (one[i][j] + onesum[i - 1][j] + zersum[i - 1][j]) % mod;
	        
	        onesum[i][j] = (onesum[i - 1][j]*(j + 1) + one[i][j]) % mod;
	        zersum[i][j] = (zersum[i - 1][j]*j + zer[i][j]) % mod;
	    }
	    
    print((one[n][m] + 5*mod) % mod);
}

















