Language: C++

#include <set>
#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <tr1/unordered_set>
#define REP(i,n) for(int i=0; i<(int)(n); i++)

typedef long long ll;

using namespace std;

inline int decode(ll data, int pos){
  return (data >> (pos * 4)) & 0xf;
}

inline ll encode(int data, int pos){
  return ((ll)data) << (pos * 4);
}

int distof(ll data, int n){
  int tmp[10] = { 0 };
  REP(i,n) tmp[decode(data, i)] = 1;
  int ret = 0; REP(i,9) ret += tmp[i + 1];
  return ret;
}

struct Data{
  ll data;
  int cnt;
  int dist;
  int n;
  Data(ll d, int c, int nn) : data(d), cnt(c), n(nn){
    dist = distof(data, n) + cnt;
  }
};

bool operator < (const Data &lhs, const Data &rhs){
  if(lhs.data != rhs.data) return lhs.dist > rhs.dist
  return lhs.cnt > rhs.cnt;
}

int main(){
  int n;
  while(cin >> n, n){
    string s1, s2;
    ll start = 0;
    vector<int> v;
    cin >> s1 >> s2;
    REP(i,n){
      int c1 = s1[i];
      int c2 = s2[i];
      v.push_back((c1 + 10 - c2) % 10);
    }
    v.erase(unique(v.begin(), v.end()), v.end());
    n = v.size();
    REP(i,n) start |= encode(v[i], i);

    priority_queue<Data> pq;
    tr1::unordered_set<ll> memo;
    pq.push(Data(start, 0, n));

    while(pq.size()){
      Data d  = pq.top(); pq.pop();
      ll data  = d.data;
      int cnt  = d.cnt;
      int dist = d.dist;
      int nn   = d.n;
      vector<int> tmp(nn); REP(i,nn) tmp[i] = decode(
      tmp.erase(unique(tmp.begin(), tmp.end()), tmp.e
      nn = tmp.size();

      if(memo.count(data)) continue;
      memo.insert(data);

      // REP(i,nn) printf("%d ", tmp[i]); printf(" :

      {
        int t[10] = {0}; REP(i,nn) t[tmp[i]]++;
        bool ok = true;
        REP(i,9) if(t[i+1] > 1) ok = false;
        if(ok){
          int ans = cnt;
          REP(i,9) ans += t[i + 1];
          printf("%d\n", ans);
          break;
        }
      }

      if(data == 0){
        printf("%d\n", cnt);
        break;
      }

      REP(i, nn) if(tmp[i]){
        REP(j, i + 1) if(tmp[j]){
          bool ok = true;
          for(int k = j + 1; k < i; k++)
            if(tmp[k] == 0) ok = false;
          if(!ok) continue;

          REP(k, 9){
            ll next = 0;
            REP(l, nn){
              if(j <= l && l <= i){
                next |= encode((tmp[l] + k + 1) % 10,
              }else{
                next |= encode(tmp[l], l);
              }
            }
            if(next == 0){
              puts("test");
              printf("%d\n", cnt + 1);
              goto end;
            }
            if(cnt + 1 + distof(next, nn) >= n) conti
            if(!memo.count(next)){
              pq.push(Data(next, cnt + 1, nn));
            }
          }
        }
        break;
      }
    }
  end:;
  }
  return 0;
}

Compiler output:
a.cc: In function 'bool operator<(const Data&, const Data&)':
a.cc:39:54: error: expected ';' before 'return'
   39 |   if(lhs.data != rhs.data) return lhs.dist > rhs.dist
      |                                                      ^
      |                                                      ;
   40 |   return lhs.cnt > rhs.cnt;
      |   ~~~~~~                                              
a.cc: In function 'int main()':
a.cc:70:53: error: 'class std::vector<int>' has no member named 'e'
   70 |       tmp.erase(unique(tmp.begin(), tmp.end()), tmp.e
      |                                                     ^
a.cc:107:15: error: expected primary-expression before '}' token
  107 |               }else{
      |               ^
a.cc:116:49: error: 'conti' was not declared in this scope; did you mean 'cnt'?
  116 |             if(cnt + 1 + distof(next, nn) >= n) conti
      |                                                 ^~~~~
      |                                                 cnt