Problem:
Problem C:
Dial Lock
A dial lock is a kind of lock which has some dials with printed numbers. It has a special sequence of
numbers, namely an unlocking sequence, to be opened.
You are working at a manufacturer of dial locks. Your job is to verify that every manufactured lock is
unlocked by its unlocking sequence. In other words, you have to rotate many dials of many many locks.
It’s a very hard and boring task. You want to reduce time to open the locks.
It’s a good idea to rotate multiple dials at one time. It is, however, a difficult problem to find steps to open
a given lock with the fewest rotations. So you decided to write a program to find such steps for given
initial and unlocking sequences.
Your company’s dial locks are composed of vertically stacked k (1 ≤
k
≤ 10) cylindrical dials. Every dial
has 10 numbers, from 0 to 9, along the side of the cylindrical shape from the left to the right in sequence.
The neighbor on the right of 9 is 0.
A dial points one number at a certain position. If you rotate a dial to the left by
i
digits, the dial newly
points the
i
-th right number. In contrast, if you rotate a dial to the right by
i
digits, it points the
i
-th left
number. For example, if you rotate a dial pointing 8 to the left by 3 digits, the dial newly points 1.
You can rotate more than one adjacent dial at one time. For example, consider a lock with 5 dials. You
can rotate just the 2nd dial. You can rotate the 3rd, 4th and 5th dials at the same time. But you cannot
rotate the 1st and 3rd dials at one time without rotating the 2nd dial. When you rotate multiple dials, you
have to rotate them to the same direction by the same digits.
Your program is to calculate the fewest number of rotations to unlock, for given initial and unlocking
sequences. Rotating one or more adjacent dials to the same direction by the same digits is counted as one
rotation.
Input
The input consists of multiple datasets. 

Each datset consists of two lines. The first line contains an integer k. The second lines contain two strings,
separated by a space, which indicate the initial and unlocking sequences.
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, print the minimum number of rotations in one line.
Sample Input
4
1357 4680
6
777777 003330
0
Output for the Sample Input
1
2

Submitted solutions:

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

using VS = vector<string>;    using LL = long long;
using VI = vector<int>;       using VVI = vector<VI>;
using PII = pair<int, int>;   using PLL = pair<LL, LL>;
using VL = vector<LL>;        using VVL = vector<VL>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
#define debug(x) cerr << #x << ": " << x << endl
const int INF = 1e9;                          const LL LINF = 1e16;
const LL MOD = 1000000007;                    const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };    int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

/* -----  2018/04/25  Problem: AOJ 2157 / Link: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2157  ----- */
/* ------問題------



-----問題ここまで----- */
/* -----解説等-----



----解説ここまで---- */


LL N;
int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);


	while (cin >> N, N) {
		string s, t; cin >> s >> t;
		VI usenum;
		LL ans = 0;
		FOR(i, 0, N) {
			s[i] -= '0';
			t[i] -= '0';
		}

		queue<string>Q;
		map<string, int>d;
		d[s] = 0;
		Q.push(s);

		while (!Q.empty()) {
			string a = Q.front(); Q.pop();
			int dist = d[a];
			if (a == t) {
				ans = dist;
				break;
			}
			int id = 0;
			while (a[id] == t[id])id++;
			int diff = (t[id] - a[id] + 10) % 10;
			FOR(j, id, N) {
				a[j] += diff;
				a[j] %= 10;
				if (d.find(a) == d.end()) {
					d[a] = dist + 1;
					Q.push(a);
				}
			}
		}
		cout << ans << endl;
	}

	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4104
final_rank: 17

code_snippet: // acade

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

int main()
{
    while (true) {
        int n;
        cin >> n;
        if (n == 0) {
            break;
        }
        string s, p;
        cin >> s >> p;
        int ans = 1e9;
        function<void(string, int)> rec = [&](string s, int cnt) {
            if (s == p) {
                ans = min(ans, cnt);
                return;
            }
            int id = 0;
            while (id < n && s[id] == p[id]) {
                id++;
            }
            int diff = ((p[id] - '0') - (s[id] - '0') + 10) % 10;
            for (int j = id; j < n; j++) {
                s[j] = ((s[j] - '0' + diff) % 10 + '0');
                rec(s, cnt + 1);
            }
        };
        rec(s, 0);
        cout << ans << '\n';
    }
}
accuracy: 1
time_ms: 770
memory_kb: 3268
final_rank: 18

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

/**
 * @brief template
 */

int main() {
while(1) {
    int N;
    cin >> N;
    if (N == 0) return 0;
    string S, T;
    cin >> S >> T;
    vector<int> A(N);
    rep(i,0,N) A[i] = ((T[i]-S[i])+10)%10;
    A.push_back(0);
    rrep(i,0,N) {
        A[i+1] = (A[i+1]-A[i]+10) % 10;
    }
    vector<int> V;
    rep(i,0,N+1) if (A[i] != 0) V.push_back(A[i]);
    N = V.size();
    vector<bool> B(1<<N,false);
    rep(i,0,1<<N) {
        int COUNT = 0;
        rep(j,0,N) {
            if (i&(1<<j)) COUNT += V[j];
        }
        if (COUNT % 10 == 0) B[i] = true;
    }
    vector<int> DP(1<<N,-inf);
    DP[0] = 0;
    rep(i,0,1<<N) {
        if (DP[i] == inf) continue;
        rep(j,1,1<<N) {
            if (B[j] && ((i&j)==0)) chmax(DP[i+j],DP[i]+1);
        }
    }
    cout << N - DP[(1<<N)-1] << endl;
}
}
accuracy: 1
time_ms: 10
memory_kb: 3216
final_rank: 1

code_snippet: #line 2 "cp-library/src/cp-template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull  = unsigned long long;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
using i128 = __int128_t;
template < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }
template < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }
template < class T, class U > T ceil (T x, U y) { return (x > 0 ? (x + y - 1) / y :           x / y); }
template < class T, class U > T floor(T x, U y) { return (x > 0 ?           x / y : (x - y + 1) / y); }
int popcnt(i32 x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(i64 x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }

#line 2 "cp-library/src/utility/rep_itr.hpp"
template < class T > struct itr_rep {
    T i, d;
    constexpr itr_rep(const T i) noexcept : i(i), d(1) {}
    constexpr itr_rep(const T i, const T d) noexcept : i(i), d(d) {}
    void operator++() noexcept { i += d; }
    constexpr int operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr_rep x) const noexcept { return d > 0 ? i < x.i : i > x.i; }
};

template < class T > struct rep {
    const itr_rep< T > s, t;
    constexpr rep(const T t) noexcept : s(0), t(t) {}
    constexpr rep(const T s, const T t) noexcept : s(s), t(t) {}
    constexpr rep(const T s, const T t, const T d) noexcept : s(s, d), t(t, d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};

template < class T > struct revrep {
    const itr_rep < T > s, t;
    constexpr revrep(const T t) noexcept : s(t - 1, -1), t(-1, -1) {}
    constexpr revrep(const T s, const T t) noexcept : s(t - 1, -1), t(s - 1, -1) {}
    constexpr revrep(const T s, const T t, const T d) noexcept : s(t - 1, -d), t(s - 1, -d) {}
    constexpr auto begin() const noexcept { return s; }
    constexpr auto end  () const noexcept { return t; }
};
#line 3 "cp-library/src/utility/io.hpp"

/* 128bit integer */
istream& operator>>(istream& is, i128& x) {
    std::string s; is >> s;
    int pm = (s[0] == '-');
    x = 0;
    for(int i : rep(pm, int(s.size()))) x = x * 10 + (s[i] - '0');
    if(pm) x *= -1;
    return is;
}
ostream& operator<<(ostream& os, const i128& x) {
    if(x == 0) return os << '0';
    i128 y = x;
    if(y < 0) { os << '-'; y *= -1; }
    std::vector<int> ny;
    while(y > 0) { ny.push_back(y % 10); y /= 10; }
    for(int i : revrep(ny.size())) os << ny[i];
    return os;
}

template < class S, class T > istream& operator>>(istream& is,       std::pair< S, T >& x) { is >> x.first        >> x.second; return is; }
template < class S, class T > ostream& operator<<(ostream& os, const std::pair< S, T >& x) { os << x.first << " " << x.second; return os; }

namespace scanner {
    struct sca {
        template < class T > operator T() {
            T s; std::cin >> s; return s;
        }
    };
    struct vec {
        int n;
        vec(int n) : n(n) {}
        template < class T > operator std::vector< T >() {
            std::vector< T > v(n);
            for(T& x : v) std::cin >> x;
            return v;
        }
    };
    struct mat {
        int h, w;
        mat(int h, int w) : h(h), w(w) {}
        template < class T > operator std::vector< std::vector< T > >() {
            std::vector m(h, std::vector< T >(w));
            for(std::vector< T >& v : m) for(T& x : v) std::cin >> x;
            return m;
        }
    };
    struct speedup {
        speedup() {
            std::cin.tie(0);
            std::ios::sync_with_stdio(0);
        }
    } speedup_instance;
}
scanner::sca in() { return scanner::sca(); }
scanner::vec in(int n) { return scanner::vec(n); }
scanner::mat in(int h, int w) { return scanner::mat(h, w); }

namespace printer {
    void precision(int d) { std::cout << std::fixed << std::setprecision(d); }
    void flush() { std::cout.flush(); }
}

template < class T >
ostream& operator<<(ostream& os, const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) { os << a[i]; if(i != n - 1) os << ' '; }
    return os;
}

int print() { std::cout << '\n'; return 0; }
template < class head, class... tail > int print(head&& h, tail&&... t) {
    std::cout << h; if(sizeof...(tail)) std::cout << ' ';
    return print(std::forward<tail>(t)...);
}
template < class T > int print_n(const std::vector< T > a) {
    int n = a.size();
    for(int i : rep(n)) std::cout << a[i] << "\n";
    return 0;
}


#line 2 "cp-library/src/utility/key_val.hpp"

template < class K, class V >
struct key_val {
    K key; V val;
    key_val() {}
    key_val(K key, V val) : key(key), val(val) {}
    template < std::size_t Index >
    std::tuple_element_t< Index, key_val >& get() {
        if constexpr (Index == 0) return key;
        if constexpr (Index == 1) return val;
    }
};

namespace std {

template < class K, class V > struct tuple_size < key_val< K, V > > : integral_constant< size_t, 2 > {};
template < class K, class V > struct tuple_element < 0, key_val< K, V > > { using type = K; };
template < class K, class V > struct tuple_element < 1, key_val< K, V > > { using type = V; };

}
#line 2 "cp-library/src/utility/vec_op.hpp"
template < class T > key_val< int, T > max_of(const vector< T >& a) {
    int i = std::max_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class T > key_val< int, T > min_of(const vector< T >& a) {
    int i = std::min_element(a.begin(), a.end()) - a.begin();
    return {i, a[i]};
}
template < class S, class T > S sum_of(const vector< T >& a) {
    S sum = 0;
    for(const T x : a) sum += x;
    return sum;
}
template < class S, class T > vector< S > freq_of(const vector< T >& a, T L, T R) {
    vector< S > res(R - L, S(0));
    for(const T x : a) res[x - L] += 1;
    return res;
}
template < class S, class T > struct prefix_sum {
    vector< S > s;
    prefix_sum(const vector< T >& a) : s(a) {
        s.insert(s.begin(), S(0));
        for(int i : rep(a.size())) s[i + 1] += s[i];
    }
    // [L, R)
    S sum(int L, int R) { return s[R] - s[L]; }
};
#line 3 "cp-library/src/utility/heap.hpp"

template < class T > using heap_min = std::priority_queue< T, std::vector< T >, std::greater< T > >;
template < class T > using heap_max = std::priority_queue< T, std::vector< T >, std::less< T > >;

#line 27 "cp-library/src/cp-template.hpp"

#line 1 "cp-library/src/algorithm/bin_search.hpp"
template < class T, class F >
T bin_search(T ok, T ng, F f) {
    while(abs(ng - ok) > 1) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}

template < class T, class F >
T bin_search_real(T ok, T ng, F f, int step = 80) {
    while(step--) {
        T mid = (ok + ng) / 2;
        (f(mid) ? ok : ng) = mid;
    }
    return ok;
}
#line 2 "cp-library/src/algorithm/argsort.hpp"

template < class T > std::vector< int > argsort(const std::vector< T > &a) {
    std::vector< int > ids((int)a.size());
    std::iota(ids.begin(), ids.end(), 0);
    std::sort(ids.begin(), ids.end(), [&](int i, int j) {
        return a[i] < a[j] || (a[i] == a[j] && i < j);
    });
    return ids;
}
#line 1 "macro.hpp"
namespace macro {

using size_type = int;
template < class container > void  sort(container& a) { std::sort(std:: begin(a), std:: end(a)); }
template < class container > void rsort(container& a) { std::sort(std::rbegin(a), std::rend(a)); }
template < class container > void reverse(container& a) { std::reverse(std::begin(a), std::end(a)); }
template < class container > void unique(container& a) {
    std::sort(std::begin(a), std::end(a));
    a.erase(std::unique(std::begin(a), std::end(a)), std::end(a));
}
template < class container > container  sorted(const container& a) { container b = a;  sort(b); return std::move(b); }
template < class container > container rsorted(const container& a) { container b = a; rsort(b); return std::move(b); }
template < class container, class compare > void sort(container& a, const compare& cmp) { std::sort(std::begin(a), std::end(a), cmp); }
template < class container, class compare > container sorted(const container& a, const compare& cmp) { container b = a; sort(b, cmp); return std::move(b); }
template < class container, class value > size_type lower_bound(const container& a, const value& x) { return std::lower_bound(std::begin(a), std::end(a), x) - std::begin(a); }
template < class container, class value > size_type upper_bound(const container& a, const value& x) { return std::upper_bound(std::begin(a), std::end(a), x) - std::begin(a); }

const std::vector<std::pair<size_type, size_type>> dir4 = { {+1,  0}, {-1,  0}, { 0, +1}, { 0, -1} };
const std::vector<std::pair<size_type, size_type>> dir8 = { {-1, -1}, {-1,  0}, {-1, +1}, { 0, -1}, { 0, +1}, {+1, -1}, {+1,  0}, {+1, +1} };

#ifdef _DEBUG
#define debug(x) std::cout << "[" << __LINE__ << "] " << #x << ": " << x << std::endl
#else
#define debug(x)
#endif

template < class container > void concat(container& a, const container& b) {
    a.insert(std::end(a), std::begin(b), std::end(b));
}
std::vector<size_type> iota(const size_type n) {
    std::vector<size_type> I(n);
    std::iota(std::begin(I), std::end(I), 0);
    return I;
}
template < class container > std::vector<size_type> sort_idx(const container& a) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return a[i] < a[j] or (a[i] == a[j] and i < j); });
    return I;
}
template < class container, class compare > std::vector<size_type> sort_idx(const container& a, const compare& cmp) {
    const size_type n = a.size();
    std::vector<size_type> I = iota(n);
    std::sort(std::begin(I), std::end(I), [&](size_type i, size_type j) { return cmp(a[i], a[j]) or (a[i] == a[j] and i < j); });
    return std::move(I);
}

struct grid {
    using size_type = int;
    size_type H, W;
    grid(const size_type H, const size_type W) : H(H), W(W) {}
    bool contains(const size_type i, const size_type j) {
        return 0 <= i and i < H and 0 <= j and j < W;
    }
};

} // namespace macro

using namespace macro;
#line 3 "A.cpp"

int solve(int n) {
    string s_ = in(), t_ = in();
    vector<int> s(n), t(n);
    for(int i : rep(n)) {
        s[i] = s_[i] - '0';
        t[i] = t_[i] - '0';
    }

    auto dfs = [&](auto self, int i) -> int {
        if(i == n) return 0;
        if(s[i] == t[i]) return self(self, i + 1);
        int min = 1e9;

        int d = t[i] - s[i];
        for(int k : rep(i, n)) {
            s[k] = (s[k] + d + 10) % 10;
            chmin(min, self(self, i + 1));
        }
        for(int k : rep(i, n)) s[k] = (s[k] - d + 10) % 10;

        return min + 1;
    };

    return dfs(dfs, 0);
}

int main() {
    while(true) {
        int k = in();
        if(k == 0) return 0;
        print(solve(k));
    }
}
accuracy: 1
time_ms: 270
memory_kb: 3324
final_rank: 16

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef pair<string, int> ps;
int rotate(char c1, char c2){
    int a1 = c1-'0';
    int a2 = c2-'0';
    return (a2+10-a1)%10;
}
int main(){
    int n;
    string sa, sb;
    while(cin >> n, n){
        cin >> sa >> sb;
        queue<ps> que;
        que.push(ps(sa, 0));
        map<string, int> mp;
        while(!que.empty()){
            ps p = que.front();
            que.pop();
            string ns = p.first;
            int ni = p.second;
            if(mp.count(ns)) continue;
            mp[ns] = ni;
            int id = ni;
            while(id<n && ns[id]==sb[id]) id++;
            if(id==n) continue;
            int rot = rotate(ns[id], sb[id]);
            for(int i=id;i<n;i++){
                ns[i] = (ns[i]-'0'+rot)%10+'0';
                que.push(ps(ns, ni+1));
            }
        }
        cout << mp[sb] << endl;
    }
    return(0);
}
accuracy: 1
time_ms: 40
memory_kb: 5648
final_rank: 19

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

const int M = 10;
vector<int> dif(12);

int rec(int d, int p, int k) {
    if (p == k) return d;
    if (d >= k) return k;
    int rot = dif[p];
    if (rot == 0) return rec(d, p + 1, k);
    int ans = rec(d + 1, p + 1, k);
    for (int i = p; i < k; i++) {
        dif[i] = (dif[i] - rot + M) % M;
        ans = min(ans, rec(d + 1, p + 1, k));
    }
    for (int i = p; i < k; i++) dif[i] = (dif[i] + rot) % M;
    return ans;
}

int main() {
    int k;
    while (cin >> k, k) {
        string s, t;
        cin >> s >> t;
        for (int i = 0; i < k; i++) {
            dif[i] = ((int)(t[i] - s[i]) + M) % M;
        }
        cout << rec(0, 0, k) << endl;
    }
}
accuracy: 1
time_ms: 1120
memory_kb: 3116
final_rank: 20

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

const int M = 10;
int k, ans;
vector<int> dif(12);

void rec(int d, int p) {
    if (p == k) {
        ans = min(ans, d);
        return;
    }
    if (d >= ans)
        return;

    int rot = dif[p];
    if (rot == 0) {
        rec(d, p + 1);
        return;
    }

    rec(d + 1, p + 1);
    for (int i = p; i < k; i++) {
        dif[i] = (dif[i] - rot + M) % M;
        rec(d + 1, p + 1);
    }
    for (int i = p; i < k; i++)
        dif[i] = (dif[i] + rot) % M;
}

int main() {
    ios_base::sync_with_stdio(false); 
    cin.tie(NULL); 
    while (cin >> k, k) {
        string s, t;
        cin >> s >> t;
        for (int i = 0; i < k; i++) {
            dif[i] = ((int)(t[i] - s[i]) + M) % M;
        }
        ans = k;
        rec(0, 0);
        cout << ans << "\n";
    }
    return 0;
}
accuracy: 1
time_ms: 280
memory_kb: 3108
final_rank: 12

code_snippet: #include <iostream>
#include <algorithm>
using namespace std;
int k, ans;
int dif[12];

const int M = 10;

void rec(int d, int p, int dif[]) {
  if (p == k) {
    ans = min(ans, d);
    return;
  }
  if (d >= ans)
    return;
  int rot = dif[p];
  if (rot == 0) {
    rec(d, p + 1, dif);
    return;
  }
  rec(d + 1, p + 1, dif);
  int difCopy[k];
  copy(dif, dif + k, difCopy);
  for (int i = p; i < k; i++) {
    difCopy[i] = (difCopy[i] - rot + M) % M;
    rec(d + 1, p + 1, difCopy);
  }
}

int main() {
  while (cin >> k, k) {
    string s, t;
    cin >> s >> t;
    for (int i = 0; i < k; i++) {
      dif[i] = ((int)(t[i] - s[i]) + M) % M;
    }
    ans = k;
    rec(0, 0, dif);
    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 290
memory_kb: 3116
final_rank: 14

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int M = 10;
int k, ans;
vector<int> dif(12);
void rec(int d, int p) {
  if (p == k || d >= ans) {
    ans = min(d, ans);
    return;
  }
  int rot = dif[p];
  if(rot != 0){
    rec(d + 1, p + 1);
    for (int i = p; i < k; ++i) {
      dif[i] = (dif[i] - rot + M) % M;
      rec(d + 1, p + 1);
    }
    for (int i = p; i < k; ++i)
      dif[i] = (dif[i] + rot) % M;
  } else {
    rec(d, p + 1);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  while (cin >> k, k) {
    string s, t;
    cin >> s >> t;
    for (int i = 0; i < k; ++i)
      dif[i] = (int(t[i] - s[i]) + M) % M;
    ans = k;
    rec(0, 0);
    cout << ans << "\n";
  }
  return 0;
}
accuracy: 1
time_ms: 290
memory_kb: 3120
final_rank: 15

code_snippet: #include <iostream>
#include <algorithm>
using namespace std;
const int M = 10;
int dif[12];

int rec(int d, int p, int k, int ans) {
  if (p == k) {
    return d;
  }
  if (d >= ans)
    return ans;
  int rot = dif[p];
  if (rot == 0) {
    return rec(d, p + 1, k, ans);
  }
  ans = min(ans, rec(d + 1, p + 1, k, ans));
  for (int i = p; i < k; i++) {
    dif[i] = (dif[i] - rot + M) % M;
    ans = min(ans, rec(d + 1, p + 1, k, ans));
  }
  for (int i = p; i < k; i++)
    dif[i] = (dif[i] + rot) % M;
  return ans;
}

int main() {
  int k;
  while (cin >> k, k) {
    string s, t;
    cin >> s >> t;
    for (int i = 0; i < k; i++) {
      dif[i] = ((int)(t[i] - s[i]) + M) % M;
    }
    cout << rec(0, 0, k, k) << endl;
  }
}
accuracy: 1
time_ms: 280
memory_kb: 3136
final_rank: 13

code_snippet: #include <iostream>
using namespace std;
int k, ans;
int dif[12];
const int M = 10;
void rec(int d, int p) {
  if (p == k) {
    ans = d;
    return;
  }
  int rot = dif[p];
  if (rot == 0) {
    rec(d, p + 1);
    return;
  }
  if (d >= ans - 1)
    return;
  rec(d + 1, p + 1);
  for (int i = p; i < k; i++) {
    dif[i] = (dif[i] - rot) % M;
    rec(d + 1, p + 1);
  }
  for (int i = p; i < k; i++)
    dif[i] = (dif[i] + rot + M) % M;
}
int main() {
  while (cin >> k, k) {
    string s, t;
    cin >> s >> t;
    for (int i = 0; i < k; i++) {
      dif[i] = ((int)(t[i] - s[i]) + M) % M;
    }
    ans = k;
    rec(0, 0);
    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 80
memory_kb: 3088
final_rank: 2

code_snippet: #include <iostream>
using namespace std;
int k, ans;
int dif[12];
const int M = 10;
void rec(int d, int p) {
  if (p == k) {
    ans = d;
    return;
  }
  if (d >= ans)
    return;
  int rot = dif[p];
  if (rot == 0) {
    rec(d, p + 1);
    return;
  }
  rec(d + 1, p + 1);
  for (int i = p; i < k; i++) {
    dif[i] = (dif[i] - rot) % M;
    rec(d + 1, p + 1);
  }
  for (int i = p; i < k; i++)
    dif[i] = (dif[i] + rot + M) % M;
}
int main() {
  while (cin >> k, k) {
    string s, t;
    cin >> s >> t;
    for (int i = 0; i < k; i++) {
      dif[i] = ((int)(t[i] - s[i]) + M) % M;
    }
    ans = k;
    rec(0, 0);
    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 210
memory_kb: 3192
final_rank: 10

code_snippet: #include <iostream>
using namespace std;
int k, ans;
int dif[12];
const int M = 10;
void rec(int d, int p) {
  if (p == k) {
    ans = d;
    return;
  }
  if (d >= ans)
    return;
  int rot = dif[p];
  if (rot == 0) {
    rec(d, p + 1);
    return;
  }
  rec(d + 1, p + 1);
  for (int i = p; i < k; i++) {
    dif[i] = (dif[i] - rot) % M;
    rec(d + 1, p + 1);
  }
  for (int i = p; i < k; i++) {
    dif[i] = (dif[i] + rot + M) % M;
  }
}
int main() {
  while (cin >> k, k) {
    string s, t;
    cin >> s >> t;
    for (int i = 0; i < k; i++) {
      dif[i] = ((int)(t[i] - s[i]) + M) % M;
    }
    ans = k;
    rec(0, 0);
    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 210
memory_kb: 3132
final_rank: 7

code_snippet: #include <iostream>
using namespace std;
int k, ans;
int dif[12];
const int M = 10;
void rec(int d, int p) {
  if (p == k) {
    ans = d;
    return;
  }
  if (d >= ans)
    return;
  int rot = dif[p];
  if (rot == 0) {
    rec(d, p + 1);
    return;
  }
  rec(d + 1, p + 1);
  for (int i = p; i < k; i++) {
    dif[i] = (dif[i] - rot) % M;
    rec(d + 1, p + 1);
  }
  for (int i = p; i < k; i++)
    dif[i] = (dif[i] + rot) % M;
}
int main() {
  while (cin >> k, k) {
    string s, t;
    cin >> s >> t;
    for (int i = 0; i < k; i++) {
      dif[i] = ((int)(t[i] - s[i]) + M) % M;
    }
    ans = k;
    rec(0, 0);
    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 210
memory_kb: 3152
final_rank: 9

code_snippet: #include <iostream>
using namespace std;
int k, ans;
int dif[12];
const int M = 10;
void rec(int d, int p) {
  if (p == k) {
    ans = d;
    return;
  }
  if (d >= ans)
    return;
  int rot = dif[p];
  if (rot == 0) {
    rec(d, p + 1);
    return;
  }
  rec(d + 1, p + 1);
  for (int i = p; i < k; i++) {
    dif[i] = (dif[i] - rot) % M;
    rec(d + 1, p + 1);
  }
  for (int i = p; i < k; i++)
    dif[i] = (dif[i] + rot + M) % M;
}
int main() {
  while (cin >> k, k) {
    string s, t;
    cin >> s >> t;
    for (int i = 0; i < k; i++) {
      dif[i] = ((t[i] - s[i]) + M) % M;
    }
    ans = k;
    rec(0, 0);
    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 210
memory_kb: 3144
final_rank: 8

code_snippet: #include <iostream>
using namespace std;
int k, ans;
int dif[11];
const int M = 10;
void rec(int d, int p) {
  if (p == k) {
    ans = d;
    return;
  }
  if (d >= ans)
    return;
  int rot = dif[p];
  if (rot == 0) {
    rec(d, p + 1);
    return;
  }
  rec(d + 1, p + 1);
  for (int i = p; i < k; i++) {
    dif[i] = (dif[i] - rot) % M;
    rec(d + 1, p + 1);
  }
  for (int i = p; i < k; i++)
    dif[i] = (dif[i] + rot + M) % M;
}
int main() {
  while (cin >> k, k) {
    string s, t;
    cin >> s >> t;
    for (int i = 0; i < k; i++) {
      dif[i] = ((int)(t[i] - s[i]) + M) % M;
    }
    ans = k;
    rec(0, 0);
    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 210
memory_kb: 3036
final_rank: 3

code_snippet: #include <iostream>
using namespace std;
int k, ans;
int dif[12];
const int M = 10;
void rec(int d, int p) {
  if (p == k) {
    ans = d;
    return;
  }
  if (d >= ans)
    return;
  int rot = dif[p];
  if (rot == 0) {
    rec(d, p + 1);
    return;
  }
  rec(d + 1, p + 1);
  for (int i = p; i < k; i++) {
    dif[i] = (dif[i] - rot) % M;
    rec(d + 1, p + 1);
  }
  for (int i = p; i < k; i++)
    dif[i] = (dif[i] + rot + M) % M;
}
int main() {
  while (cin >> k, k) {
    string s, t;
    cin >> s >> t;
    for (int i = 0; i < k; i++)
      dif[i] = ((int)(t[i] - s[i]) + M) % M;
    ans = k;
    rec(0, 0);
    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 210
memory_kb: 3068
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
int k, ans;
int dif[12];
const int M = 10;
void rec(int d, int p) {
  if (p == k) {
    ans = d;
    return;
  }
  if (d >= ans)
    return;
  int rot = dif[p];
  if (rot == 0) {
    rec(d, p + 1);
    return;
  }
  rec(d + 1, p + 1);
  for (int i = p; i < k; i++) {
    dif[i] = (dif[i] - rot) % M;
    rec(d + 1, p + 1);
  }
  for (int i = p; i < k; i++)
    dif[i] = (dif[i] + rot + M) % M;
}
int main() {
  while (cin >> k, k) {
    string s, t;
    cin >> s >> t;
    for (int i = 0; i < k; i++) {
      dif[i] = ((int)(t[i] - s[i]) + M) % M;
    }
    ans = k;
    rec(0, 0);
    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 210
memory_kb: 3048
final_rank: 4

code_snippet: #include <iostream>
#include <cstring> // added to use memset function
using namespace std;
int k, ans;
int dif[12];
const int M = 10;

void rec(int d, int p, int *temp_dif) { // added temp_dif array
  if (p == k) {
    ans = d;
    return;
  }
  if (d >= ans)
    return;
  int rot = temp_dif[p]; // using temp_dif instead of dif
  if (rot == 0) {
    rec(d, p + 1, temp_dif); // passing temp_dif instead of dif
    return;
  }
  rec(d + 1, p + 1, temp_dif); // passing temp_dif instead of dif
  for (int i = p; i < k; i++) {
    temp_dif[i] = (temp_dif[i] - rot) % M; // using temp_dif instead of dif
    rec(d + 1, p + 1, temp_dif); // passing temp_dif instead of dif
  }
  for (int i = p; i < k; i++)
    temp_dif[i] = (temp_dif[i] + rot + M) % M; // using temp_dif instead of dif
}

int main() {
  while (cin >> k, k) {
    string s, t;
    cin >> s >> t;
    int temp_dif[12]; // creating temp_dif array
    memset(temp_dif, 0, sizeof(temp_dif)); // initializing temp_dif to 0
    for (int i = 0; i < k; i++) {
      temp_dif[i] = ((int)(t[i] - s[i]) + M) % M; // using temp_dif instead of dif
    }
    ans = k;
    rec(0, 0, temp_dif); // passing temp_dif instead of dif
    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 280
memory_kb: 3088
final_rank: 11

code_snippet: #include <iostream>
#include <cstring>
using namespace std;
const int MAXK = 12;
int k, ans;
int dif[MAXK];
const int M = 10;
void rec(int d, int p, int* dif) {
  if (p == k) {
    ans = d;
    return;
  }
  if (d >= ans)
    return;
  int rot = dif[p];
  if (rot == 0) {
    rec(d, p + 1, dif);
    return;
  }
  int new_dif[MAXK];
  memcpy(new_dif, dif, sizeof(new_dif));
  rec(d + 1, p + 1, new_dif);
  for (int i = p; i < k; i++) {
    new_dif[i] = (new_dif[i] - rot) % M;
    if (new_dif[i] < 0)
      new_dif[i] += M;
    rec(d + 1, p + 1, new_dif);
  }
}
int main() {
  while (cin >> k, k) {
    string s, t;
    cin >> s >> t;
    for (int i = 0; i < k; i++) {
      dif[i] = ((int)(t[i] - s[i]) + M) % M;
    }
    ans = k;
    rec(0, 0, dif);
    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 230
memory_kb: 3056
final_rank: 6