Problem:
テンキー (Tenkey)
問題文
JOI 君はテンキーを
1
つ持っている．このテンキーには
0
から
9
までの数字が印字されているキーが以下の図のように配置されている．
2
が印字されたキーの下，および
3
が印字されたキーの下にはキーは存在しないことに注意せよ．
またこのテンキーには，テンキーに配置されているキーのうち
1
つのキーを指し示すカーソルが存在している．カーソルは最初
0
が印字されているキーを指し示している．
JOI 君は
1
回の操作で次のうちのいずれかを選んで行うことができる．
カーソルを，現在カーソルが指し示しているキーと上下左右に隣接しているキーに移動させる．ただし，キーが存在しない場所にカーソルを移動させることはできない．
キーを押す．すなわち，カーソルが指し示しているキーに印字されている数字を入力する．この際，以前の操作によってすでに数字が入力されていた場合，すでに入力されていた数字のすぐ右に新たな数字が入力される．
いま，JOI 君はこのテンキーを使って，
M
で割った余りが
R
であるような正の整数を入力したいと考えている．テンキーの操作には時間がかかるので，なるべく少ない操作回数で入力したい．
M
と
R
が与えられるので，JOI 君が行う必要のある操作の回数の最小値を求めるプログラムを作成せよ．
制約
2 ≦ M ≦ 100 000
．
1 ≦ R ＜ M
．
入力される値はすべて整数である．
入力
入力は以下の形式で標準入力から与えられる．
M
R
出力
M
で割った余りが
R
であるような正の整数を入力するために必要な操作の回数の最小値を
1
行で出力せよ．
入出力例
入力例 1
100000 13
出力例 1
5
この例では，以下の
5
回の操作を行うことによって
13
を入力することが可能である．
4
回以下の操作によって条件を満たす整数を入力することは不可能であるので，
5
を出力する．
カーソルを上に移動させる．カーソルが指し示すキーは
1
となる．
キーを押す．
1
が入力される．
カーソルを右に移動させる．カーソルが指し示すキーは
2
となる．
カーソルを右に移動させる．カーソルが指し示すキーは
3
となる．
キーを押す．
3
が新たに入力され，これまでに入力された数字は
13
となる．
入力例 2
4 3
出力例 2
3
この例では，
3
回の操作を行うことによって
11
を入力することが可能である．
3
を入力するには
4
回以上の操作を行わなければならず，最適ではないことに注意せよ．
情報オリンピック日本委員会
作 『第 19 回日本情報オリンピック JOI 2019/2020 一次予選競技課題』

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
#define all(A) A.begin(),A.end()
#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)


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;
    }
}



int main() {

    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    vvll D={{1},{0,2,4},{1,3,5},{2,6},{1,5,7},{2,4,6,8},{3,5,9},{4,8},{5,7,9},{6,8}};
    ll M,R;
    cin>>M>>R;
    vvll DP(10,vll(M,1e18));
    DP[0][0]=0;
    queue<pair<ll,ll>> Q;
    vector<vector<bool>> seen(10,vector<bool>(M,0));
    Q.push({0,0});
    while(!Q.empty()){
        ll p=Q.front().first;
        ll r=Q.front().second;
        Q.pop();
        if(seen[p][r])continue;
        if(!seen[p][(r*10+p)%M]){
            if(chmin(DP[p][(r*10+p)%M],DP[p][r]+1)){
                Q.push({p,(r*10+p)%M});
            }
        }
        for(auto v:D[p]){
            if(seen[v][r])continue;
            if(chmin(DP[v][r],DP[p][r]+1)){
                Q.push({v,r});
            }
        }
    }
    ll an=1e18;
    rep(i,10)chmin(an,DP[i][R]);
    cout<<an<<endl;
}
accuracy: 1
time_ms: 30
memory_kb: 16332
final_rank: 2

code_snippet: #line 1 "a.cpp"
#define PROBLEM ""
#line 2 "/home/kuhaku/home/github/algo/lib/graph/graph.hpp"
#include <iostream>
#include <vector>

/**
 * @brief 重み付きグラフ
 *
 * @tparam T 辺の重みの型
 */
template <class T>
struct Graph {
  private:
    struct _edge {
        constexpr _edge() : _from(), _to(), _weight() {}
        constexpr _edge(int from, int to, T weight) : _from(from), _to(to), _weight(weight) {}
        constexpr bool operator<(const _edge &rhs) const { return weight() < rhs.weight(); }
        constexpr bool operator>(const _edge &rhs) const { return rhs < *this; }

        constexpr int from() const { return _from; }
        constexpr int to() const { return _to; }
        constexpr T weight() const { return _weight; }

      private:
        int _from, _to;
        T _weight;
    };

  public:
    using edge_type = typename Graph<T>::_edge;

    Graph() : _size(), edges() {}
    Graph(int v) : _size(v), edges(v) {}

    const auto &operator[](int i) const { return edges[i]; }
    auto &operator[](int i) { return edges[i]; }
    const auto begin() const { return edges.begin(); }
    auto begin() { return edges.begin(); }
    const auto end() const { return edges.end(); }
    auto end() { return edges.end(); }
    constexpr int size() const { return _size; }

    void add_edge(const edge_type &e) { edges[e.from()].emplace_back(e); }
    void add_edge(int from, int to, T weight = T(1)) { edges[from].emplace_back(from, to, weight); }
    void add_edges(int from, int to, T weight = T(1)) {
        edges[from].emplace_back(from, to, weight);
        edges[to].emplace_back(to, from, weight);
    }

    void input_edge(int m, int base = 1) {
        for (int i = 0; i < m; ++i) {
            int from, to;
            T weight;
            std::cin >> from >> to >> weight;
            add_edge(from - base, to - base, weight);
        }
    }
    void input_edges(int m, int base = 1) {
        for (int i = 0; i < m; ++i) {
            int from, to;
            T weight;
            std::cin >> from >> to >> weight;
            add_edges(from - base, to - base, weight);
        }
    }

  private:
    int _size;
    std::vector<std::vector<edge_type>> edges;
};

template <>
struct Graph<void> {
  private:
    struct _edge {
        constexpr _edge() : _from(), _to() {}
        constexpr _edge(int from, int to) : _from(from), _to(to) {}

        constexpr int from() const { return _from; }
        constexpr int to() const { return _to; }
        constexpr int weight() const { return 1; }
        constexpr bool operator<(const _edge &rhs) const { return weight() < rhs.weight(); }
        constexpr bool operator>(const _edge &rhs) const { return rhs < *this; }

      private:
        int _from, _to;
    };

  public:
    using edge_type = typename Graph<void>::_edge;

    Graph() : _size(), edges() {}
    Graph(int v) : _size(v), edges(v) {}

    const auto &operator[](int i) const { return edges[i]; }
    auto &operator[](int i) { return edges[i]; }
    const auto begin() const { return edges.begin(); }
    auto begin() { return edges.begin(); }
    const auto end() const { return edges.end(); }
    auto end() { return edges.end(); }
    constexpr int size() const { return _size; }

    void add_edge(const edge_type &e) { edges[e.from()].emplace_back(e); }
    void add_edge(int from, int to) { edges[from].emplace_back(from, to); }
    void add_edges(int from, int to) {
        edges[from].emplace_back(from, to);
        edges[to].emplace_back(to, from);
    }

    void input_edge(int m, int base = 1) {
        for (int i = 0; i < m; ++i) {
            int from, to;
            std::cin >> from >> to;
            add_edge(from - base, to - base);
        }
    }
    void input_edges(int m, int base = 1) {
        for (int i = 0; i < m; ++i) {
            int from, to;
            std::cin >> from >> to;
            add_edges(from - base, to - base);
        }
    }

  private:
    int _size;
    std::vector<std::vector<edge_type>> edges;
};
#line 2 "/home/kuhaku/home/github/algo/lib/template/template.hpp"
#pragma GCC target("sse4.2,avx2,bmi2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
template <class T, class U>
constexpr bool chmax(T &a, const U &b) {
    return a < (T)b ? a = (T)b, true : false;
}
template <class T, class U>
constexpr bool chmin(T &a, const U &b) {
    return (T)b < a ? a = (T)b, true : false;
}
constexpr std::int64_t INF = 1000000000000000003;
constexpr int Inf = 1000000003;
constexpr double EPS = 1e-7;
constexpr double PI = M_PI;
#line 4 "/home/kuhaku/home/github/algo/lib/graph/dijkstra.hpp"

/**
 * @brief ダイクストラ法
 *
 * @tparam T 辺の重みの型
 * @param g グラフ
 * @param s 始点
 * @param inf 正の無限表現
 * @retval std::vector<T> 各頂点までの最短距離
 */
template <class T>
std::vector<T> dijkstra(const Graph<T> &g, int s = 0, T inf = std::numeric_limits<T>::max()) {
    struct _node {
        constexpr _node() : _to(), _dist() {}
        constexpr _node(int to, T dist) : _to(to), _dist(dist) {}
        constexpr bool operator<(const _node &rhs) const { return this->dist() < rhs.dist(); }
        constexpr bool operator>(const _node &rhs) const { return rhs < *this; }

        constexpr int to() const { return this->_to; }
        constexpr T dist() const { return this->_dist; }

      private:
        int _to;
        T _dist;
    };
    std::vector<T> dists(g.size(), inf);
    std::priority_queue<_node, std::vector<_node>, std::greater<>> p_que;
    dists[s] = T();
    p_que.emplace(s, T());
    while (!p_que.empty()) {
        auto node = p_que.top();
        p_que.pop();
        if (dists[node.to()] < node.dist()) continue;
        for (auto &e : g[node.to()]) {
            if (chmin(dists[e.to()], node.dist() + e.weight()))
                p_que.emplace(e.to(), node.dist() + e.weight());
        }
    }
    return dists;
}

std::vector<int> dijkstra(const Graph<void> &g, int s = 0,
                          int inf = std::numeric_limits<int>::max()) {
    std::vector<int> dists(g.size(), inf);
    std::queue<int> que;
    dists[s] = 0;
    que.emplace(s);
    while (!que.empty()) {
        auto index = que.front();
        que.pop();
        for (auto &e : g[index]) {
            if (chmin(dists[e.to()], dists[index] + 1)) que.emplace(e.to());
        }
    }
    return dists;
}
#line 3 "/home/kuhaku/home/github/algo/lib/template/macro.hpp"
#define FOR(i, m, n) for (int i = (m); i < int(n); ++i)
#define FORR(i, m, n) for (int i = (m)-1; i >= int(n); --i)
#define FORL(i, m, n) for (int64_t i = (m); i < int64_t(n); ++i)
#define rep(i, n) FOR (i, 0, n)
#define repn(i, n) FOR (i, 1, n + 1)
#define repr(i, n) FORR (i, n, 0)
#define repnr(i, n) FORR (i, n + 1, 1)
#define all(s) (s).begin(), (s).end()
#line 3 "/home/kuhaku/home/github/algo/lib/template/sonic.hpp"
struct Sonic {
    Sonic() {
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        std::cout << std::fixed << std::setprecision(20);
    }

    constexpr void operator()() const {}
} sonic;
#line 5 "/home/kuhaku/home/github/algo/lib/template/atcoder.hpp"
using namespace std;
using ll = std::int64_t;
using ld = long double;
template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
    return is >> p.first >> p.second;
}
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
    for (T &i : v) is >> i;
    return is;
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
    return os << '(' << p.first << ',' << p.second << ')';
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
    for (auto it = v.begin(); it != v.end(); ++it) {
        os << (it == v.begin() ? "" : " ") << *it;
    }
    return os;
}
template <class Head, class... Tail>
void co(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cout << head << '\n';
    else std::cout << head << ' ', co(std::forward<Tail>(tail)...);
}
template <class Head, class... Tail>
void ce(Head &&head, Tail &&...tail) {
    if constexpr (sizeof...(tail) == 0) std::cerr << head << '\n';
    else std::cerr << head << ' ', ce(std::forward<Tail>(tail)...);
}
template <typename T, typename... Args>
auto make_vector(T x, int arg, Args... args) {
    if constexpr (sizeof...(args) == 0) return std::vector<T>(arg, x);
    else return std::vector(arg, make_vector<T>(x, args...));
}
void Yes(bool is_correct = true) {
    std::cout << (is_correct ? "Yes" : "No") << '\n';
}
void No(bool is_not_correct = true) {
    Yes(!is_not_correct);
}
void YES(bool is_correct = true) {
    std::cout << (is_correct ? "YES" : "NO") << '\n';
}
void NO(bool is_not_correct = true) {
    YES(!is_not_correct);
}
void Takahashi(bool is_correct = true) {
    std::cout << (is_correct ? "Takahashi" : "Aoki") << '\n';
}
void Aoki(bool is_not_correct = true) {
    Takahashi(!is_not_correct);
}
#line 5 "a.cpp"

int main(void) {
    Graph<void> ten_key(10);
    ten_key.add_edges(0, 1);
    ten_key.add_edges(1, 2);
    ten_key.add_edges(1, 4);
    ten_key.add_edges(2, 3);
    ten_key.add_edges(2, 5);
    ten_key.add_edges(3, 6);
    ten_key.add_edges(4, 5);
    ten_key.add_edges(4, 7);
    ten_key.add_edges(5, 6);
    ten_key.add_edges(5, 8);
    ten_key.add_edges(6, 9);
    ten_key.add_edges(7, 8);
    ten_key.add_edges(8, 9);

    vector<vector<int>> d(10);
    rep (i, 10) d[i] = dijkstra(ten_key, i);

    int m, r;
    cin >> m >> r;
    Graph<int> g(m * 10);
    rep (i, m) {
        rep (j, 10) {
            rep (k, 10) {
                g.add_edge(i * 10 + j, (i * 10 + k) % m * 10 + k, d[j][k] + 1);
            }
        }
    }

    auto dist = dijkstra(g);
    ll ans = INF;
    rep (i, 10) chmin(ans, dist[r * 10 + i]);
    co(ans);

    return 0;
}
accuracy: 1
time_ms: 400
memory_kb: 243280
final_rank: 12

code_snippet: /* -*- coding: utf-8 -*-
 *
 * 0675.cc: テンキー (Tenkey)
 */

#include<cstdio>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_M = 100000;
const int CNT = 30;
const int INF = 1 << 29;

//                  0  1  2  3  4  5  6  7  8  9
const int kxs[] = { 0, 0, 1, 2, 0, 1, 2, 0, 1, 2 };
const int kys[] = { 0, 1, 1, 1, 2, 2, 2, 3, 3, 3 };

/* typedef */

/* global variables */

int es[10][10], dp[2][10][MAX_M];

/* subroutines */

inline void setmin(int &a, int b) { if (a > b) a = b; }

/* main */

int main() {
  for (int i = 0; i < 10; i++)
    for (int j = 0; j < 10; j++)
      es[i][j] = abs(kxs[j] - kxs[i]) + abs(kys[j] - kys[i]) + 1;

  int m, r;
  scanf("%d%d", &m, &r);

  for (int i = 0; i < 10; i++) fill(dp[0][i], dp[0][i] + m, INF);
  dp[0][0][0] = 0;
  int cur = 0, nxt = 1;

  int mind = INF;
  for (int cnt = 0; cnt < CNT; cnt++) {
    for (int i = 0; i < 10; i++) fill(dp[nxt][i], dp[nxt][i] + m, INF);

    for (int i = 0; i < 10; i++)
      for (int j = 0; j < m; j++)
	if (dp[cur][i][j] < INF)
	  for (int k = 0; k < 10; k++)
	    setmin(dp[nxt][k][(j * 10 + k) % m], dp[cur][i][j] + es[i][k]);

    for (int i = 0; i < 10; i++) setmin(mind, dp[nxt][i][r]);

    swap(cur, nxt);
  }

  printf("%d\n",  mind);
  return 0;
}
accuracy: 1
time_ms: 630
memory_kb: 10784
final_rank: 10

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cassert>
#include <chrono>
#include <random>
#include <set>
#include <utility>
#include <array>
#include <bitset>
#include <unordered_set>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

vector<int> ten[10] = {
        {1},
        {0,2,4},
        {1,5,3},
        {2,6},
        {1,5,7},
        {2,4,6,8},
        {3,5,9},
        {4,8},
        {5,7,9},
        {6,8}
}; 

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int m,r; cin >> m >> r;
    vector<vector<int>> dp(10, vector<int>(m, 1e9));
    dp[0][0] = 0;
    queue<pair<int,int>> q;
    q.push({0, 0});
    while (q.size()) {
        auto s = q.front(); q.pop();
        int cost = dp[s.first][s.second];
        if (s.second == r) {
            cout << cost << endl;
            break;
        }
        // move
        int d = s.first;
        for (int i : ten[d]) {
            if (dp[i][s.second] == 1e9) {
                dp[i][s.second] = cost+1;
                q.push({i, s.second});
            }
        }
        // push
        int nxt = (10*s.second+d)%m;
        if (dp[d][nxt] == 1e9) {
            dp[d][nxt] = cost+1;
            q.push({d, nxt});
        }
    }
}
accuracy: 1
time_ms: 20
memory_kb: 9752
final_rank: 1

code_snippet: #include<bits/stdc++.h>
using namespace std;
int dp[14][100000][10];
int sosa(int a, int b){
    int res = 1+abs((a+2)/3-(b+2)/3);
    if(a==0) a=1;
    if(b==0) b=1;
    return  res + abs((a+2)%3-(b+2)%3);
}
int main(){
    int m, r, ans=32;
    scanf("%d %d", &m, &r);
    for(int i=0;i<m;i++){
        for(int j=0;j<10;j++) dp[0][i][j] = 32;
    }
    for(int i=1;i<10;i++){
        dp[0][i%m][i] = sosa(0, i);
        if(i%m == r) ans = min(ans, sosa(0, i));
    }
    for(int i=0;i<13;i++){
        fill_n(*dp[i+1], m*10, 32);
        for(int j=0;j<m;j++){
            for(int k=0;k<10;k++){
                int next = (j*10+k)%m;
                for(int l=0;l<10;l++){
                    int move = dp[i][j][l] + sosa(l, k);
                    if(move>=ans || dp[i+1][next][k]<=move) continue;
                    dp[i+1][next][k] = move;
                    if(next == r) ans = min(ans, move);
                }
            }
        }
    }
    printf("%d\n", ans);
    return(0);
}
accuracy: 1
time_ms: 380
memory_kb: 57924
final_rank: 7

code_snippet: #include<bits/stdc++.h>
using namespace std;
int dp[14][100000][10];
int sosa(int a, int b){
    int res = 1+abs((a+2)/3-(b+2)/3);
    if(a==0) a=1;
    if(b==0) b=1;
    return  res + abs((a+2)%3-(b+2)%3);
}
int main(){
    int m, r, ans=32;
    scanf("%d %d", &m, &r);
    for(int i=0;i<m;i++){
        for(int j=0;j<10;j++) dp[0][i][j] = 32;
    }
    for(int i=1;i<10;i++){
        dp[0][i%m][i] = sosa(0, i);
        if(i%m == r) ans = min(ans, sosa(0, i));
    }
    for(int i=0;i<13;i++){
        fill_n(*dp[i+1], m*10, 32);
        for(int j=0;j<m;j++){
            for(int k=0;k<10;k++){
                int next = (j*10+k)%m;
                for(int l=0;l<10;l++){
                    int move = dp[i][j][l] + sosa(l, k);
                    if(move>=ans && dp[i+1][next][k]<=move) continue;
                    dp[i+1][next][k] = move;
                    if(next == r) ans = min(ans, move);
                }
            }
        }
    }
    printf("%d\n", ans);
    return(0);
}
accuracy: 0.7450980392156863
time_ms: 480
memory_kb: 57904
final_rank: 19

code_snippet: #include<bits/stdc++.h>
using namespace std;
int dp[14][100000][10];
int sosa(int a, int b){
    int res = 1+abs((a+2)/3-(b+2)/3);
    if(a==0) a=1;
    if(b==0) b=1;
    return  res + abs((a+2)%3-(b+2)%3);
}
int main(){
    int m, r, ans=32;
    cin >> m >> r;
    for(int i=0;i<m;i++){
        for(int j=0;j<10;j++) dp[0][i][j] = 32;
    }
    for(int i=1;i<10;i++){
        dp[0][i%m][i] = sosa(0, i);
        if(i%m == r) ans = min(ans, sosa(0, i));
    }
    for(int i=0;i<13;i++){
        fill_n(*dp[i+1], m*10, 32);
        for(int j=0;j<m;j++){
            for(int k=0;k<10;k++){
                int next = (j*10+k)%m;
                for(int l=0;l<10;l++){
                    int move = dp[i][j][l] + sosa(l, k);
                    if(move>=ans) continue;
                    dp[i+1][next][k] = min(dp[i+1][next][k], move);
                    if(next == r) ans = min(ans, dp[i+1][r][k]);
                }
            }
        }
    }
    cout << ans << endl;
    return(0);
}
accuracy: 1
time_ms: 360
memory_kb: 57824
final_rank: 6

code_snippet: #include<bits/stdc++.h>
using namespace std;
int dp[14][100000][10];
int sosa(int a, int b){
    int res = 1+abs((a+2)/3-(b+2)/3);
    if(a==0) a=1;
    if(b==0) b=1;
    return  res + abs((a+2)%3-(b+2)%3);
}
int main(){
    int m, r, ans=32;
    cin >> m >> r;
    for(int i=0;i<m;i++){
        for(int j=0;j<10;j++) dp[0][i][j] = 32;
    }
    for(int i=1;i<10;i++){
        dp[0][i%m][i] = sosa(0, i);
        if(i%m == r) ans = min(ans, sosa(0, i));
    }
    for(int i=0;i<13;i++){
        fill_n(*dp[i+1], m*10, 32);
        for(int j=0;j<m;j++){
            for(int k=0;k<10;k++){
                int next = (j*10+k)%m;
                for(int l=0;l<10;l++){
                    int move = dp[i][j][l] + sosa(l, k);
                    dp[i+1][next][k] = min(dp[i+1][next][k], move);
                    if(next == r) ans = min(ans, dp[i+1][r][k]);
                }
            }
        }
    }
    cout << ans << endl;
    return(0);
}
accuracy: 1
time_ms: 420
memory_kb: 57824
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;
int dp[2][100000][10];
int sosa(int a, int b){
    int res = 1+abs((a+2)/3-(b+2)/3);
    if(a==0) a=1;
    if(b==0) b=1;
    return  res + abs((a+2)%3-(b+2)%3);
}
int main(){
    int m, r, ans=32;
    cin >> m >> r;
    for(int i=0;i<m;i++){
        for(int j=0;j<10;j++){
            dp[0][i][j] = 32;
            dp[1][i][j] = 32;
        }
    }
    for(int i=1;i<10;i++){
        dp[0][i%m][i] = sosa(0, i);
        if(i%m == r) ans = min(ans, sosa(0, i));
    }
    for(int i=0;i<12;i++){
        fill_n(*dp[(i+1)%2], m*10, 32);
        for(int j=0;j<m;j++){
            for(int k=0;k<10;k++){
                int next = (j*10+k)%m;
                for(int l=0;l<10;l++){
                    int move = dp[i%2][j][l] + sosa(l, k);
                    dp[(i+1)%2][next][k] = min(dp[(i+1)%2][next][k], move);
                }
                if(next == r) ans = min(ans, dp[(i+1)%2][next][k]);
            }
        }
    }
    cout << ans << endl;
    return(0);
}
accuracy: 0.7647058823529411
time_ms: 360
memory_kb: 10948
final_rank: 18

code_snippet: #include<bits/stdc++.h>
using namespace std;
int dp[2][100000][10];
int sosa(int a, int b){
    int res = 1+abs((a+2)/3-(b+2)/3);
    if(a==0) a=1;
    if(b==0) b=1;
    return  res + abs((a+2)%3-(b+2)%3);
}
int main(){
    int m, r, ans=32;
    cin >> m >> r;
    for(int i=0;i<m;i++){
        for(int j=0;j<10;j++){
            dp[0][i][j] = 32;
            dp[1][i][j] = 32;
        }
    }
    for(int i=1;i<10;i++){
        dp[0][i%m][i] = sosa(0, i);
        if(i%m == r) ans = min(ans, sosa(0, i));
    }
    for(int i=0;i<13;i++){
        fill_n(*dp[(i+1)%2], m*10, 32);
        for(int j=0;j<m;j++){
            for(int k=0;k<10;k++){
                int next = (j*10+k)%m;
                for(int l=0;l<10;l++){
                    int move = dp[i%2][j][l] + sosa(l, k);
                    dp[(i+1)%2][next][k] = min(dp[(i+1)%2][next][k], move);
                }
                if(next == r) ans = min(ans, dp[(i+1)%2][next][k]);
            }
        }
    }
    cout << ans << endl;
    return(0);
}
accuracy: 1
time_ms: 390
memory_kb: 10924
final_rank: 4

code_snippet: #include<bits/stdc++.h>
using namespace std;
int dp[2][100000][10];
int sosa(int a, int b){
    int res = 1+abs((a+2)/3-(b+2)/3);
    if(a==0) a=1;
    if(b==0) b=1;
    return  res + abs((a+2)%3-(b+2)%3);
}
int main(){
    int m, r, ans=32;
    cin >> m >> r;
    for(int i=0;i<m;i++){
        for(int j=0;j<10;j++){
            dp[0][i][j] = 32;
            dp[1][i][j] = 32;
        }
    }
    for(int i=1;i<10;i++){
        dp[0][i%m][i] = sosa(0, i);
        if(i%m == r) ans = min(ans, sosa(0, i));
    }
    for(int i=0;i<14;i++){
        fill_n(*dp[(i+1)%2], m*10, 32);
        for(int j=0;j<m;j++){
            for(int k=0;k<10;k++){
                int next = (j*10+k)%m;
                for(int l=0;l<10;l++){
                    int move = dp[i%2][j][l] + sosa(l, k);
                    dp[(i+1)%2][next][k] = min(dp[(i+1)%2][next][k], move);
                }
                if(next == r) ans = min(ans, dp[(i+1)%2][next][k]);
            }
        }
    }
    cout << ans << endl;
    return(0);
}
accuracy: 1
time_ms: 420
memory_kb: 10924
final_rank: 5

code_snippet: #include<bits/stdc++.h>
using namespace std;
int dp[2][100000][10];
int sosa(int a, int b){
    int res = 1+abs((a+2)/3-(b+2)/3);
    if(a==0) a=1;
    if(b==0) b=1;
    return  res + abs((a+2)%3-(b+2)%3);
}
int main(){
    int m, r, ans=32;
    cin >> m >> r;
    for(int i=0;i<m;i++){
        for(int j=0;j<10;j++){
            dp[0][i][j] = 32;
            dp[1][i][j] = 32;
        }
    }
    for(int i=1;i<10;i++){
        dp[0][i%m][i] = sosa(0, i);
        if(i%m == r) ans = min(ans, sosa(0, i));
    }
    for(int i=0;i<10;i++){
        fill_n(*dp[(i+1)%2], m*10, 32);
        for(int j=0;j<m;j++){
            for(int k=0;k<10;k++){
                int next = (j*10+k)%m;
                for(int l=0;l<10;l++){
                    int move = dp[i%2][j][l] + sosa(l, k);
                    dp[(i+1)%2][next][k] = min(dp[(i+1)%2][next][k], move);
                }
                if(next == r) ans = min(ans, dp[(i+1)%2][next][k]);
            }
        }
    }
    cout << ans << endl;
    return(0);
}
accuracy: 0.7647058823529411
time_ms: 300
memory_kb: 10948
final_rank: 15

code_snippet: #include<bits/stdc++.h>
using namespace std;
int dp[2][100000][10];
int sosa(int a, int b){
    int res = 1+abs((a+2)/3-(b+2)/3);
    if(a==0) a=1;
    if(b==0) b=1;
    return  res + abs((a+2)%3-(b+2)%3);
}
int main(){
    int m, r, ans=32;
    cin >> m >> r;
    for(int i=0;i<m;i++){
        for(int j=0;j<10;j++){
            dp[0][i][j] = 32;
            dp[1][i][j] = 32;
        }
    }
    for(int i=1;i<14;i++){
        dp[0][i%m][i] = sosa(0, i);
        if(i%m == r) ans = min(ans, sosa(0, i));
    }
    for(int i=0;i<10;i++){
        fill_n(*dp[(i+1)%2], m*10, 32);
        for(int j=0;j<m;j++){
            for(int k=0;k<10;k++){
                int next = (j*10+k)%m;
                for(int l=0;l<10;l++){
                    int move = dp[i%2][j][l] + sosa(l, k);
                    dp[(i+1)%2][next][k] = min(dp[(i+1)%2][next][k], move);
                }
                if(next == r) ans = min(ans, dp[(i+1)%2][next][k]);
            }
        }
    }
    cout << ans << endl;
    return(0);
}
accuracy: 0.7647058823529411
time_ms: 300
memory_kb: 10948
final_rank: 15

code_snippet: #include<bits/stdc++.h>
using namespace std;
int dp[2][100000][10];
int sosa(int a, int b){
    int res = 1+abs((a+2)/3-(b+2)/3);
    if(a==0) a=1;
    if(b==0) b=1;
    return  res + abs((a+2)%3-(b+2)%3);
}
int main(){
    int m, r, ans=32;
    cin >> m >> r;
    for(int i=0;i<m;i++){
        for(int j=0;j<10;j++){
            dp[0][i][j] = 32;
            dp[1][i][j] = 32;
        }
    }
    for(int i=1;i<13;i++){
        dp[0][i%m][i] = sosa(0, i);
        if(i%m == r) ans = min(ans, sosa(0, i));
    }
    for(int i=0;i<10;i++){
        fill_n(*dp[(i+1)%2], m*10, 32);
        for(int j=0;j<m;j++){
            for(int k=0;k<10;k++){
                int next = (j*10+k)%m;
                for(int l=0;l<10;l++){
                    int move = dp[i%2][j][l] + sosa(l, k);
                    dp[(i+1)%2][next][k] = min(dp[(i+1)%2][next][k], move);
                }
                if(next == r) ans = min(ans, dp[(i+1)%2][next][k]);
            }
        }
    }
    cout << ans << endl;
    return(0);
}
accuracy: 0.7647058823529411
time_ms: 300
memory_kb: 10924
final_rank: 14

code_snippet: #include<bits/stdc++.h>
using namespace std;
int dp[2][100000][10];
int sosa(int a, int b){
    int res = 1+abs((a+2)/3-(b+2)/3);
    if(a==0) a=1;
    if(b==0) b=1;
    return  res + abs((a+2)%3-(b+2)%3);
}
int main(){
    int m, r, ans=32;
    cin >> m >> r;
    for(int i=0;i<m;i++){
        for(int j=0;j<10;j++){
            dp[0][i][j] = 32;
            dp[1][i][j] = 32;
        }
    }
    for(int i=1;i<12;i++){
        dp[0][i%m][i] = sosa(0, i);
        if(i%m == r) ans = min(ans, sosa(0, i));
    }
    for(int i=0;i<10;i++){
        fill_n(*dp[(i+1)%2], m*10, 32);
        for(int j=0;j<m;j++){
            for(int k=0;k<10;k++){
                int next = (j*10+k)%m;
                for(int l=0;l<10;l++){
                    int move = dp[i%2][j][l] + sosa(l, k);
                    dp[(i+1)%2][next][k] = min(dp[(i+1)%2][next][k], move);
                }
                if(next == r) ans = min(ans, dp[(i+1)%2][next][k]);
            }
        }
    }
    cout << ans << endl;
    return(0);
}
accuracy: 0.7647058823529411
time_ms: 300
memory_kb: 10948
final_rank: 15

code_snippet: #include<bits/stdc++.h>
using namespace std;
int dp[2][100000][10];
int sosa(int a, int b){
    int res = 1+abs((a+2)/3-(b+2)/3);
    if(a==0) a=1;
    if(b==0) b=1;
    return  res + abs((a+2)%3-(b+2)%3);
}
int main(){
    int m, r, ans=32;
    cin >> m >> r;
    for(int i=0;i<m;i++){
        for(int j=0;j<10;j++){
            dp[0][i][j] = 32;
            dp[1][i][j] = 32;
        }
    }
    for(int i=1;i<10;i++){
        dp[0][i%m][i] = sosa(0, i);
        if(i%m == r) ans = min(ans, sosa(0, i));
    }
    for(int i=0;i<15;i++){
        fill_n(*dp[(i+1)%2], m*10, 32);
        for(int j=0;j<m;j++){
            for(int k=0;k<10;k++){
                int next = (j*10+k)%m;
                for(int l=0;l<10;l++){
                    int move = dp[i%2][j][l] + sosa(l, k);
                    dp[(i+1)%2][next][k] = min(dp[(i+1)%2][next][k], move);
                }
                if(next == r) ans = min(ans, dp[(i+1)%2][next][k]);
            }
        }
    }
    cout << ans << endl;
    return(0);
}
accuracy: 1
time_ms: 450
memory_kb: 10924
final_rank: 8

code_snippet: #include<bits/stdc++.h>
using namespace std;
int dp[2][100000][10];
int sosa(int a, int b){
    int res = 1+abs((a+2)/3-(b+2)/3);
    if(a==0) a=1;
    if(b==0) b=1;
    return  res + abs((a+2)%3-(b+2)%3);
}
int main(){
    int m, r, ans=33;
    cin >> m >> r;
    for(int i=0;i<m;i++){
        for(int j=0;j<10;j++){
            dp[0][i][j] = 33;
            dp[1][i][j] = 33;
        }
    }
    for(int i=1;i<10;i++){
        dp[0][i%m][i] = sosa(0, i);
        if(i%m == r) ans = min(ans, sosa(0, i));
    }
    for(int i=0;i<20;i++){
        fill_n(*dp[(i+1)%2], m*10, 33);
        for(int j=0;j<m;j++){
            for(int k=0;k<10;k++){
                int next = (j*10+k)%m;
                for(int l=0;l<10;l++){
                    int move = dp[i%2][j][l] + sosa(l, k);
                    if(dp[(i+1)%2][next][k] > move){
                        dp[(i+1)%2][next][k] = move;
                    }
                }
                if(next == r) ans = min(ans, dp[(i+1)%2][next][k]);
            }
        }
    }
    cout << ans << endl;
    return(0);
}
accuracy: 1
time_ms: 630
memory_kb: 10920
final_rank: 11

code_snippet: #include<bits/stdc++.h>
using namespace std;
int dp[2][100000][10];
int sosa(int a, int b){
    int res = 1+abs((a+2)/3-(b+2)/3);
    if(a==0) a=1;
    if(b==0) b=1;
    return  res + abs((a+2)%3-(b+2)%3);
}
int main(){
    int m, r, ans=33;
    cin >> m >> r;
    for(int i=0;i<m;i++){
        for(int j=0;j<10;j++){
            dp[0][i][j] = 33;
            dp[1][i][j] = 33;
        }
    }
    for(int i=1;i<10;i++){
        dp[0][i%m][i] = sosa(0, i);
        if(i%m == r) ans = min(ans, sosa(0, i));
    }
    for(int i=0;i<20;i++){
        for(int j=0;j<m;j++){
            for(int k=0;k<10;k++){
                int next = (j*10+k)%m;
                dp[(i+1)%2][next][k] = 33;
                for(int l=0;l<10;l++){
                    int move = dp[i%2][j][l] + sosa(l, k);
                    if(dp[(i+1)%2][next][k] > move){
                        dp[(i+1)%2][next][k] = move;
                    }
                }
                if(next == r) ans = min(ans, dp[(i+1)%2][next][k]);
            }
        }
    }
    cout << ans << endl;
    return(0);
}
accuracy: 0.058823529411764705
time_ms: 600
memory_kb: 10840
final_rank: 20

code_snippet: #include <iostream>
#include <vector>
#include <cmath>
#include <stdlib.h>
#include <string.h>

using namespace std;
struct E{
	int x,y;
};
int dp[10][100002];
vector<E> vs;
int as[10][10];
int main() {
	E e;
	e.x=0;
	e.y=3;
	vs.push_back(e);
	for(int i=8;i>=0;i--){
		e.y=i/3;
		e.x=2-i%3;
		vs.push_back(e);
	}
	
	for(int i=0;i<vs.size();i++){
		E e1=vs[i];
		for(int j=0;j<vs.size();j++){
			E e2=vs[j];
			int t=abs(e1.x-e2.x)+abs(e1.y-e2.y)+1;
			as[i][j]=t;
			//cout<<" "<<t;
		}
		//cout<<endl;
	}
	int cr=-1,cd=-1;
	memset(dp,-1,sizeof(dp));
	dp[0][0]=0;
	int m,r;
	cin>>m>>r;
	int z=0;
	int s1=1;
	while(true){
		cd=-1;
		for(int i=0;i<m;i++){
			for(int n1=0;n1<10;n1++){
				if(dp[n1][i]==-1)continue;
				
				for(int n2=s1;n2<10;n2++){
					int i2=(i*10+n2)%m;
					int t2=dp[n1][i]+as[n1][n2];
					if(dp[n2][i2]==-1 || dp[n2][i2]>t2){
						dp[n2][i2]=t2;
						if(i2==r){
							if(cr==-1||cr>t2)cr=t2;
						}else{
							if(cd==-1||cd>t2)cd=t2;
						}
					}
				}
				if(s1==1 && dp[0][0]==0)dp[0][0]=-1;
				s1=0;
			}
		}
		
		if((cd==-1 && cr!=-1) ||(cr!=-1 && cr<=cd))break;
		//cout<<cr<<" "<<cd<<endl;
	}
	cout<<cr<<endl;
	return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 7056
final_rank: 3

code_snippet: //#define _GLIBCXX_DEBUG

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

#define endl '\n'
#define lfs cout<<fixed<<setprecision(10)
#define ALL(a)  (a).begin(),(a).end()
#define ALLR(a)  (a).rbegin(),(a).rend()
#define UNIQUE(a) (a).erase(unique((a).begin(),(a).end()),(a).end())
#define spa << " " <<
#define fi first
#define se second
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define EB emplace_back
#define rep(i,n,m) for(ll i = (n); i < (ll)(m); i++)
#define rrep(i,n,m) for(ll i = (ll)(m) - 1; i >= (ll)(n); i--)
using ll = long long;
using ld = long double;
const ll MOD1 = 1e9+7;
const ll MOD9 = 998244353;
const ll INF = 1e18;
using P = pair<ll, ll>;
template<typename T> using PQ = priority_queue<T>;
template<typename T> using QP = priority_queue<T,vector<T>,greater<T>>;
template<typename T1, typename T2>bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}
template<typename T1, typename T2>bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
ll median(ll a,ll b, ll c){return a+b+c-max({a,b,c})-min({a,b,c});}
void ans1(bool x){if(x) cout<<"Yes"<<endl;else cout<<"No"<<endl;}
void ans2(bool x){if(x) cout<<"YES"<<endl;else cout<<"NO"<<endl;}
void ans3(bool x){if(x) cout<<"Yay!"<<endl;else cout<<":("<<endl;}
template<typename T1,typename T2>void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;}  
template<typename T1,typename T2,typename T3>void anss(T1 x,T2 y,T3 z){ans(x!=y,x,z);};  
template<typename T>void debug(const T &v,ll h,ll w,string sv=" "){for(ll i=0;i<h;i++){cout<<v[i][0];for(ll j=1;j<w;j++)cout<<sv<<v[i][j];cout<<endl;}};
template<typename T>void debug(const T &v,ll n,string sv=" "){if(n!=0)cout<<v[0];for(ll i=1;i<n;i++)cout<<sv<<v[i];cout<<endl;};
template<typename T>void debug(const vector<T>&v){debug(v,v.size());}
template<typename T>void debug(const vector<vector<T>>&v){for(auto &vv:v)debug(vv,vv.size());}
template<typename T>void debug(stack<T> st){while(!st.empty()){cout<<st.top()<<" ";st.pop();}cout<<endl;}
template<typename T>void debug(queue<T> st){while(!st.empty()){cout<<st.front()<<" ";st.pop();}cout<<endl;}
template<typename T>void debug(deque<T> st){while(!st.empty()){cout<<st.front()<<" ";st.pop_front();}cout<<endl;}
template<typename T>void debug(PQ<T> st){while(!st.empty()){cout<<st.top()<<" ";st.pop();}cout<<endl;}
template<typename T>void debug(QP<T> st){while(!st.empty()){cout<<st.top()<<" ";st.pop();}cout<<endl;}
template<typename T>void debug(const set<T>&v){for(auto z:v)cout<<z<<" ";cout<<endl;}
template<typename T>void debug(const multiset<T>&v){for(auto z:v)cout<<z<<" ";cout<<endl;}
template<typename T,size_t size>void debug(const array<T, size> &a){for(auto z:a)cout<<z<<" ";cout<<endl;}
template<typename T,typename V>void debug(const map<T,V>&v){for(auto z:v)cout<<"["<<z.first<<"]="<<z.second<<",";cout<<endl;}
template<typename T>vector<vector<T>>vec(ll x, ll y, T w){vector<vector<T>>v(x,vector<T>(y,w));return v;}
ll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;}
vector<ll>dx={1,-1,0,0,1,1,-1,-1};vector<ll>dy={0,0,1,-1,1,-1,1,-1};
template<typename T>vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
template<typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v(ts...))>(a,make_v(ts...));}
template<typename T1, typename T2>ostream &operator<<(ostream &os, const pair<T1, T2>&p){return os << p.first << " " << p.second;}
template<typename T>ostream &operator<<(ostream &os, const vector<T> &v){for(auto &z:v)os << z << " ";cout<<"|"; return os;}
template<typename T>void rearrange(vector<int>&ord, vector<T>&v){
  auto tmp = v;
  for(int i=0;i<tmp.size();i++)v[i] = tmp[ord[i]];
}
template<typename Head, typename... Tail>void rearrange(vector<int>&ord,Head&& head, Tail&&... tail){
  rearrange(ord, head);
  rearrange(ord, tail...);
}
template<typename T> vector<int> ascend(const vector<T>&v){
  vector<int>ord(v.size());iota(ord.begin(),ord.end(),0);
  sort(ord.begin(),ord.end(),[&](int i,int j){return make_pair(v[i],i)<make_pair(v[j],j);});
  return ord;
}
template<typename T> vector<int> descend(const vector<T>&v){
  vector<int>ord(v.size());iota(ord.begin(),ord.end(),0);
  sort(ord.begin(),ord.end(),[&](int i,int j){return make_pair(v[i],-i)>make_pair(v[j],-j);});
  return ord;
}
template<typename T> vector<T> inv_perm(const vector<T>&ord){
  vector<T>inv(ord.size());
  for(int i=0;i<ord.size();i++)inv[ord[i]] = i;
  return inv;
}
ll FLOOR(ll n,ll div){assert(div>0);return n>=0?n/div:(n-div+1)/div;}
ll CEIL(ll n,ll div){assert(div>0);return n>=0?(n+div-1)/div:n/div;}
ll digitsum(ll n){ll ret=0;while(n){ret+=n%10;n/=10;}return ret;}
ll modulo(ll n,ll d){return (n%d+d)%d;};
template<typename T>T min(const vector<T>&v){return *min_element(v.begin(),v.end());}
template<typename T>T max(const vector<T>&v){return *max_element(v.begin(),v.end());}
template<typename T>T acc(const vector<T>&v){return accumulate(v.begin(),v.end(),T(0));};
template<typename T>T reverse(const T &v){return T(v.rbegin(),v.rend());};
//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
int popcount(ll x){return __builtin_popcountll(x);};
int poplow(ll x){return __builtin_ctzll(x);};
int pophigh(ll x){return 63 - __builtin_clzll(x);};
template<typename T>T poll(queue<T> &q){auto ret=q.front();q.pop();return ret;};
template<typename T>T poll(priority_queue<T> &q){auto ret=q.top();q.pop();return ret;};
template<typename T>T poll(QP<T> &q){auto ret=q.top();q.pop();return ret;};
template<typename T>T poll(stack<T> &s){auto ret=s.top();s.pop();return ret;};
ll MULT(ll x,ll y){if(LLONG_MAX/x<=y)return LLONG_MAX;return x*y;}
ll POW2(ll x, ll k){ll ret=1,mul=x;while(k){if(mul==LLONG_MAX)return LLONG_MAX;if(k&1)ret=MULT(ret,mul);mul=MULT(mul,mul);k>>=1;}return ret;}
ll POW(ll x, ll k){ll ret=1;for(int i=0;i<k;i++){if(LLONG_MAX/x<=ret)return LLONG_MAX;ret*=x;}return ret;}
template< typename T = int >
struct edge {
  int to;
  T cost;
  int id;
  edge():id(-1){};
  edge(int to, T cost = 1, int id = -1):to(to), cost(cost), id(id){}
  operator int() const { return to; }
};

template<typename T>
using Graph = vector<vector<edge<T>>>;
template<typename T>
Graph<T>revgraph(const Graph<T> &g){
  Graph<T>ret(g.size());
  for(int i=0;i<g.size();i++){
    for(auto e:g[i]){
      int to = e.to;
      e.to = i;
      ret[to].push_back(e);
    }
  }
  return ret;
}
template<typename T>
Graph<T> readGraph(int n,int m,int indexed=1,bool directed=false,bool weighted=false){
  Graph<T> ret(n);
  for(int es = 0; es < m; es++){
    int u,v;
    T w=1;
    cin>>u>>v;u-=indexed,v-=indexed;
    if(weighted)cin>>w;
    ret[u].emplace_back(v,w,es);
    if(!directed)ret[v].emplace_back(u,w,es);
  }
  return ret;
}
template<typename T>
Graph<T> readParent(int n,int indexed=1,bool directed=true){
  Graph<T>ret(n);
  for(int i=1;i<n;i++){
    int p;cin>>p;
    p-=indexed;
    ret[p].emplace_back(i);
    if(!directed)ret[i].emplace_back(p);
  }
  return ret;
}
template<typename G>
vector<ll> dijkstra(G &g,const vector<int>&s) {
  using PT=pair<ll,int>;
  priority_queue<PT, vector<PT>, greater<PT> > que;
  vector<ll>dist(g.size(),INF);
  for(auto v:s){
    dist[v] = 0;
    que.emplace(0, v);
  }
  while (!que.empty()) {
    PT p = que.top();
    que.pop();
    int v = p.second;
    if (dist[v] < p.first) continue;
    for(auto e:g[v]) {
      if (dist[e.to] > dist[v] + e.cost) {
          dist[e.to] = dist[v] + e.cost;
          que.emplace(dist[e.to], e.to);
      }
    }
  }
  return dist;
}
template<typename G>
vector<ll>dijkstra(G &g,int s=0){
  return dijkstra(g,vector<int>(1,s));
}
template<typename T>
vector<vector<T>>WarshallFloyd(vector<vector<T>>&matrix){
  vector<vector<T>>ret=matrix;
  ll sz=ret[0].size();
  for(ll i=0;i<sz;i++)ret[i][i]=0;
  for(ll j=0;j<sz;j++){
    for(ll i=0;i<sz;i++){
      for(ll o=0;o<sz;o++){
        if(ret[i][j]!=INF&&ret[j][o]!=INF){
          chmin(ret[i][o],ret[i][j]+ret[j][o]);
        }
      }
    }
  }
  return ret;
}
template<typename T>
vector<vector<ll>> WarshallFloyd(Graph<T>&g){
  ll n=g.size();
  vector<vector<ll>>ret(n,vector<ll>(n,INF));
  for(ll i=0;i<n;i++){
    ret[i][i]=0;
    for(auto e:g[i]){
      chmin(ret[i][e.to],e.cost);
    }
  }
  return WarshallFloyd(ret);
} 
int main(){
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  ll res=0,buf=0;
  bool judge = true;
	ll m,r;cin>>m>>r;
	vector<vector<ll>>v={{7,8,9},{4,5,6},{1,2,3},{0,-1,-1}};
	Graph<ll>gt(10);
	rep(i,0,4)rep(j,0,3){
		if(v[i][j]==-1)continue;
		rep(o,0,4){
			ll ni=i+dx[o],nj=j+dy[o];
			if(ni<0||nj<0||ni>=4||nj>=3||v[ni][nj]==-1)continue;
			gt[v[i][j]].EB(v[ni][nj],1);
		}
	}
	auto d=WarshallFloyd(gt);
	Graph<ll>g(m*10);
	rep(i,0,m){
		rep(j,0,10){
			rep(o,0,10){
				g[i*10+j].EB(((i*10+o)%m)*10+o,d[j][o]+1);
			}
		}
	}
	auto dd=dijkstra(g,0);
	ll ret=INF;
	rep(i,0,10)chmin(ret,dd[r*10+i]);
	cout<<ret<<endl;
  return 0;
}
accuracy: 1
time_ms: 560
memory_kb: 443264
final_rank: 13