alexjercan/bugnet huggingface dataset:
Problem p00208 (C++) — buggy submission, verdict: Time Limit Exceeded

/*#include <iostream>

unsigned long long int func(unsigned long long int a);

unsigned long long answ[1000000000];

int main(){
        unsigned long long int n;
        while(std::cin >> n, n){
                unsigned long long int ans = 0;
                unsigned long long int n_=n;
                while(n_--){
                        ans++;
                        unsigned long long int b = func(ans);
                        ans+=b;
                        n-=b;
                }
                std::cout << ans << std::endl;
        }
        return 0;
}

unsigned long long int func(unsigned long long int a){
        int res=1;
        while(a > 0){
                if(a % 10 == 4 || a % 10 == 6)return res;
                a /= 10;
                res*=10;
        }
        return 0;
}*/

#include <iostream>
#include <stack>
#define _m_ 1

const int Ebit[8] = {0, 1, 2, 3, 5, 7, 8, 9};

int main() {
  int n;
  std::stack<int> st;
  while (_m_) {
    std::cin >> n;
    if (n == 0)
      break;
    while (_m_) {
      if (n < 8) {
        st.push(Ebit[n]);
        break;
      }
      int amari = n % 8;
      n /= 8;
      st.push(Ebit[amari]);
    }
    while (st.size()) {
      std::cout << st.top();
      st.pop();
    }
    std::cout << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

/*#include <iostream>

unsigned long long int func(unsigned long long int a);

unsigned long long answ[1000000000];

int main(){
        unsigned long long int n;
        while(std::cin >> n, n){
                unsigned long long int ans = 0;
                unsigned long long int n_=n;
                while(n_--){
                        ans++;
                        unsigned long long int b = func(ans);
                        ans+=b;
                        n-=b;
                }
                std::cout << ans << std::endl;
        }
        return 0;
}

unsigned long long int func(unsigned long long int a){
        int res=1;
        while(a > 0){
                if(a % 10 == 4 || a % 10 == 6)return res;
                a /= 10;
                res*=10;
        }
        return 0;
}*/

#include <iostream>
#include <stack>
#define _m_ 1

const int Ebit[8] = {0, 1, 2, 3, 5, 6, 8, 9};

int main() {
  int n;
  std::stack<int> st;
  while (_m_) {
    std::cin >> n;
    if (n == 0)
      break;
    while (_m_) {
      if (n < 8) {
        st.push(Ebit[n]);
        break;
      }
    }
    int amari = n % 8;
    n /= 8;
    st.push(Ebit[amari]);
  }
  while (st.size()) {
    std::cout << st.top();
    st.pop();
  }
  std::cout << std::endl;
  return 0;
}
Problem p00208 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
using namespace std;

int main() {
  int set[] = {0, 1, 2, 3, 5, 7, 8, 9};
  int n;
  long ans, keta;
  while (1) {
    cin >> n;
    if (n == 0) {
      break;
    }
    ans = 0;
    keta = 1;
    while (n != 0) {
      ans += keta * set[n % 8];
      keta *= 10;
      n /= 8;
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int main() {
  int n, i;
  int count;
  while (1) {
    cin >> n;
    count = 0;
    if (n == 0) {
      break;
    }
    i = 0;
    count = 1;
    while (1) {
      if (to_string(count).find("4", 0) == -1 &&
          to_string(count).find("6", 0) == -1) {
        i++;
      }
      if (i == n) {
        cout << count << endl;
        break;
      }

      count++;
    }
  }
}
Problem p00208 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

int main() {
  int n, i, c;
  char s[16];
  while (scanf("%d", &n), n) {
    sprintf(s, "%o", n);
    for (i = 0; c = s[i]; i++) {
      c -= 48;
      printf("%d", c + c / 4 + c / 5);
    }
    printf("\\n");
  }
}

Error: TLE

Fixed submission:

#include <stdio.h>

int main() {
  int n, i, c;
  char s[16];
  while (scanf("%d", &n)) {
    sprintf(s, "%o", n);
    for (i = 0; c = s[i]; i++) {
      c -= 48;
      printf("%d", c + c / 4 + c / 5);
    }
    printf("\\n");
  }
}
Problem p00208 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <stdio.h>
using namespace std;
const int a[8] = {0, 1, 2, 3, 5, 7, 8, 9};
int n, s[15];
int main() {
  while (true) {
    scanf("%d", &n);
    if (n == 0)
      break;
    for (int i = 0; i < 15; i++) {
      s[i] = a[n % 8];
      n /= 8;
    }
    reverse(s, s + 15);
    bool flag = false;
    for (int i = 0; i < 15; i++) {
      flag = flag || s[i];
      if (flag) {
        printf("%d", s[i]);
      }
    }
    printf("\\n");
  }
  return 0;
}

Error: -11

Fixed submission:

#include <stdio.h>
const int a[8] = {0, 1, 2, 3, 5, 7, 8, 9};
int n;
int main() {
  while (true) {
    scanf("%d", &n);
    if (n == 0)
      break;
    bool flag = false;
    while (n != 0) {
      int val = n / 8;
      flag = flag || val;
      if (flag) {
        printf("%d", a[val]);
      }
      n /= 8;
    }
    printf("\\n");
  }
  return 0;
}
Problem p00208 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
using namespace std;
int x[8] = {0, 1, 2, 3, 5, 7, 8, 9};
int n, ok, k;
int main() {
  while (true) {
    ok = 0;
    cin >> n;
    if (!n) {
      break;
    }
    for (int i = 10; i >= 0; i--) {
      k = (n / (int)pow(8, i)) % 8;
      if (k > 0) {
        ok = 1;
      }
      if (ok == 1) {
        cout << x[k];
      }
    }
    cout << endl;
  }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;
int x[8] = {0, 1, 2, 3, 5, 7, 8, 9};
int n, ok, k;
int main() {
  while (true) {
    ok = 0;
    cin >> n;
    if (!n) {
      break;
    }
    for (int i = 10; i >= 0; i++) {
      k = (n / (int)pow(8, i)) % 8;
      if (k > 0) {
        ok = 1;
      }
      if (ok == 1) {
        cout << x[k];
      }
    }
    cout << endl;
  }
}
Problem p00208 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const int INF = 100000000;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
using namespace std;
typedef pair<int, int> P;
typedef long long ll;

const int MAX_N = 1e9 + 9;
vector<int> ans;

bool ok(int a) {
  while (a) {
    if (a % 10 == 4 || a % 10 == 6)
      return false;
    a /= 10;
  }
  return true;
}

int n;
string s[] = {"0", "1", "2", "3", "5", "7", "8", "9"};
void hoge() {
  ans.reserve(100000);
  rep(i, 101) {
    if (!i)
      continue;
    if (ok(i))
      ans.pb(i);
    // if(log10(i) == floor(log10(i)))
  }
  cout << endl;
  rep(i, ans.size()) {
    // if((i+1)%8==0)
    cout << i + 1 << " " << ans[i] << endl;
  }
}

int main() {

  while (cin >> n) {
    if (!n)
      break;
    string ans;
    while (n) {
      ans += s[n % 8];
      n >>= 3;
    }
    reverse(all(ans));
    cout << ans << endl;
  }

  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const int INF = 100000000;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
using namespace std;
typedef pair<int, int> P;
typedef long long ll;

const int MAX_N = 1000000009;
vector<int> ans;

bool ok(int a) {
  while (a) {
    if (a % 10 == 4 || a % 10 == 6)
      return false;
    a /= 10;
  }
  return true;
}
void init() {
  ans.reserve(MAX_N);
  rep(i, MAX_N) {
    if (!i)
      continue;
    if (ok(i))
      ans.pb(i);
  }
}
int main() {
  init();
  int n;
  while (cin >> n) {
    if (!n)
      break;
    cout << ans[n - 1] << endl;
  }
  return 0;
}
Problem p00208 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  while (1) {
    int a, j = 0;
    cin >> a;
    if (a == 0)
      break;
    j = a;
    string s = "";
    int b = 0;
    for (b = 1; b * 8 <= a; b *= 8) {
    }
    for (int c = b; c != 1; c /= 8) {
      int o = a / c;
      switch (a / c) {
      case 0:
      case 1:
      case 2:
      case 3:
        cout << a / c;
        break;
      case 4:
        cout << 5;
        break;
      default:
        cout << a / c + 2;
        break;
      }

      a -= c * o;
    }
    switch (j % 8) {
    case 0:
    case 1:
    case 2:
    case 3:
      cout << j % 8;
      break;
    case 4:
      cout << 5;
      break;
    default:
      cout << j % 8 + 2;
    }

    cout << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  while (1) {
    int a, j = 0;
    cin >> a;
    j = a;
    string s = "";
    int b = 0;
    for (b = 1; b * 8 <= a; b *= 8) {
    }
    for (int c = b; c != 1; c /= 8) {
      int o = a / c;
      switch (a / c) {
      case 0:
      case 1:
      case 2:
      case 3:
        cout << a / c;
        break;
      case 4:
        cout << 5;
        break;
      default:
        cout << a / c + 2;
        break;
      }

      a -= c * o;
    }
    switch (j % 8) {
    case 0:
    case 1:
    case 2:
    case 3:
      cout << j % 8;
      break;
    case 4:
      cout << 5;
      break;
    default:
      cout << j % 8 + 2;
    }

    cout << endl;
  }
}
Problem p00208 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define INF (1 << 29)
#define all(c) (c).begin(), (c).end()
#define pb(c) push_back(c)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

int getv(int x) {
  if (x == 4)
    return 5;
  return (x < 4) ? x : x + 2;
}

void convert(int n) {
  if (n == 0)
    return;
  convert(n / 8);
  cout << getv(n % 8);
}

int main() {
  ios::sync_with_stdio(false);
  int n;

  while (cin >> n && n) {
    convert(n);
    cout << endl;
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define INF (1 << 29)
#define all(c) (c).begin(), (c).end()
#define pb(c) push_back(c)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

int getv(int x) {
  if (x = 4)
    return 5;
  return (x < 4) ? x : x + 2;
}

void convert(int n) {
  if (n = 0)
    return;
  convert(n / 8);
  cout << getv(n % 8);
}

int main() {
  ios::sync_with_stdio(false);
  int n;

  while (cin >> n && n) {
    convert(n);
    cout << endl;
  }
}
Problem p00208 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

int main() {
  int n, num[100];
  while (cin >> n, n) {
    int k = -1;
    do {
      num[++k] = n % 8;
      if (num[k] >= 5)
        num[k] += 2;
      else if (num[k] >= 4)
        ++num[k];
    } while (n /= 8);
    for (int i = k; i >= 0; --i)
      cout << num[i];
    cout << endl;
  }
  return 0;
}

Error: 1

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n, num[100];
  while (cin >> n, n) {
    int k = -1;
    do {
      num[++k] = n % 8;
      if (num[k] >= 5)
        num[k] += 2;
      else if (num[k] >= 4)
        ++num[k];
    } while (n /= 8);
    for (int i = k; i >= 0; --i)
      cout << num[i];
    cout << endl;
  }
  return 1;
}
Problem p00208 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

typedef long long ll;

int cnv(int n) {
  if (n <= 3)
    return n;
  else {
    if (n == 4)
      return 5;
    else
      return n + 2;
  }
}

void dfs(int num) {
  if (num == 0)
    return;
  dfs(num / 8);
  cout << cnv(num % 8);
}

int main() {

  int n;
  while (cin >> n && n != 0) {
    dfs(n);
    cout << endl;
  }

  return 0;
}

// int fourOrSixPlace(ll num){
//	int idx=-1;
//	int cnt=0;
//	while(num>0){
//		if(num%10==6||num%10==4){
//			idx=cnt;
//		}
//		num/=10;
//		cnt++;
//	}
//	return idx;
// }
//
// int main(){
//
//	//int cc=0;
//	//for(ll i = 1; i <= 100000; i++){
//	//	int ret=fourOrSixPlace(i);
//	//	if(ret!=-1)
//	//		cc++;
//	//}
//	//cout<<cc<<endl;
//	ll n;
//	while(cin>>n&&n!=0){
//		ll cnum=0;
//		ll cnt=0;
//		while(1){
//
//			cnum+=100000;
//			if(cnum>n)
//				break;
//			int ret=fourOrSixPlace(cnum);
//			if(ret==-1){
//				n+=67232;
//			}
//			else{
//				n+=67232;
//				cnum+=(ll)pow(10.0,ret);
//				n+=(ll)pow(10.0,ret);
//			}
//		}
//
//		if(true){
//			cnum-=100000;
//			cnum++;
//
//			for(ll i = cnum; i <= n; i++){
//				int ret=fourOrSixPlace(i);
//				if(ret!=-1){
//					if(ret==0){
//						n++;
//					}
//					else{
//						n+=(ll)pow(10.0,ret);
//						i+=(ll)pow(10.0,ret);
//					}
//				}
//			}
//		}
//		cout<<n<<endl;
//	}
//
//
//	return 0;
// }

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

typedef long long ll;

int fourOrSixPlace(ll num) {
  int idx = -1;
  int cnt = 0;
  while (num > 0) {
    if (num % 10 == 6 || num % 10 == 4) {
      idx = cnt;
    }
    num /= 10;
    cnt++;
  }
  return idx;
}

int main() {

  // int cc=0;
  // for(ll i = 1; i <= 100000; i++){
  //	int ret=fourOrSixPlace(i);
  //	if(ret!=-1)
  //		cc++;
  // }
  // cout<<cc<<endl;
  ll n;
  while (cin >> n && n != 0) {
    ll cnum = 0;
    ll cnt = 0;
    while (1) {
      if (cnum > 10000000000LL) {
        return 0;
      }
      cnum += 100000;
      if (cnum > n)
        break;
      int ret = fourOrSixPlace(cnum);
      if (ret == -1) {
        n += 67232;
      } else {
        n += 67232;
        cnum += (ll)pow(10.0, ret);
        n += (ll)pow(10.0, ret);
      }
    }

    if (true) {
      cnum -= 100000;
      cnum++;
      for (ll i = cnum; i <= n; i++) {
        int ret = fourOrSixPlace(i);
        if (ret != -1) {
          if (ret == 0) {
            n++;
          } else {
            n += (ll)pow(10.0, ret);
            i += (ll)pow(10.0, ret);
          }
        }
      }
    }
    cout << n << endl;
  }

  return 0;
}
Problem p00208 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int t[8] = {0, 1, 2, 3, 5, 7, 8, 9};

void solve(int);

int main() {

  int n;

  while (cin >> n) {
    if (!n) {
      break;
    }
    solve(n);
    cout << endl;
  }

  return 0;
}

void solve(int n) {

  if (n >= 8) {
    solve(n / 8);
  }

  cout << t[n % 8];
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <string>

using namespace std;

int main() {

  long long int n, r;
  string s = "";
  char x[sizeof(int)];

  while (cin >> n) {
    if (!n)
      break;
    r = 0;

    for (int i = 0; i < n; i++) {
      while (1) {
        r++;
        sprintf(x, "%d", r);
        if (!(strchr(x, '4') != NULL) && !(strchr(x, '6') != NULL))
          break;
      }
    }
    cout << x << endl;
  }

  return 0;
}
Problem p00208 (C++) — buggy submission, verdict: Time Limit Exceeded

// #include <iostream>
// #include <string>
#include <stdio.h>

// using namespace std;

/*long long a[21]={0, 298790200, 797380500, 1095170800, 1392951000, 1891731200,
2190321500, 2589111800, 2987902000, 3285592200, 3783282500, 5082072800,
5380853000, 5879533200, 7178223500, 7577013800, 7973805000, 8272395200,
8771185500, 9059975800, 9358757000}; char b[100000]={};

long long int number(int n){
  long long int h=0;
  int p=0;

  p=(n/50000000)*50000000;
  h=a[p/50000000];

  while(p<n){
    h++;
    if(b[h%100000]==1 && b[h/100000]==1)
      p++;
    }

  return h;
}


int main() {
    ios::sync_with_stdio(false);
    int s;
    int m=0;
    while(m<=99999){
      int t=m;
      while(t>0){
        if(t%10==4 || t%10==6)
          {b[m]--;break;}
        else{
          t=t/10;
        }
      }
      b[m]++;
      m++;
    }

    int p,q;

    while (1) {
      cin >> s;
      if(s != 0){
        cout << number(s) << endl;
      }
      else
        break;
    }

    return 0;
}
*/

char str[15];

char *number(int n) {
  sprintf(str, "%o", n);
  int i;
  for (i = 0; i < 15; i++) {
    if (str[i] >= '4') {
      str[i]++;
    }
    if (str[i] >= '6')
      str[i]++;
  }

  return str;
}

int main() {
  int s;
  while (1) {
    scanf("%d", &s);
    if (s != 0) {
      // cout << number(s) << endl;
      printf("%s\\n", number(s));
    } else
      break;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>

using namespace std;

long long a[21] = {0,          298790200,  797380500,  1095170800, 1392951000,
                   1891731200, 2190321500, 2589111800, 2987902000, 3285592200,
                   3783282500, 5082072800, 5380853000, 5879533200, 7178223500,
                   7577013800, 7973805000, 8272395200, 8771185500, 9059975800,
                   9358757000};
char b[100000] = {};

long long int number(int n) {
  long long int h = 0;
  int p = 0;

  p = (n / 50000000) * 50000000;
  h = a[p / 50000000];

  while (p < n) {
    h++;
    if (b[h % 100000] == 1 && b[h / 100000] == 1)
      p++;
  }

  return h;
}

int main() {
  ios::sync_with_stdio(false);
  int s;
  int m = 0;
  while (m <= 99999) {
    int t = m;
    while (t > 0) {
      if (t % 10 == 4 || t % 10 == 6) {
        b[m]--;
        break;
      } else {
        t = t / 10;
      }
    }
    b[m]++;
    m++;
  }

  int p, q;

  while (1) {
    cin >> s;
    if (s != 0) {
      cout << number(s) << endl;
    } else
      break;
  }

  return 0;
}
Problem p00208 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
int main() {
  int nums[] = {0, 1, 2, 3, 5, 7, 8, 9};
  while (1) {
    int n;
    long long a = 0, d = 1;
    scanf("%d", &n);
    if (!n)
      break;
    while (n) {
      a += nums[n % 8] * d;
      d *= 10;
      n /= 8;
    }
    printf("%lld\\n", a);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main() {
  while (1) {
    int n, ans = 0;
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      ans++;
      stringstream ss;
      ss << ans;
      string buf = ss.str();
      while (buf.find('4') != string::npos || buf.find('6') != string::npos) {
        ans++;
        ss.str("");
        ss << ans;
        buf = ss.str();
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00481 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstring>
#include <queue>
#include <utility>
using namespace std;

#define INF 10000000

typedef pair<int, int> P;

queue<P> que;
char map[1000][1001];
int vst[1000][1000];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int H, W, N, sx, sy, sum = 0;

int main() {
  scanf("%d %d %d", &H, &W, &N);
  for (int i = 0; i < H; i++) {
    scanf("%s", map[i]);
    char *ptr = strchr(map[i], 'S');
    if (ptr != NULL)
      sx = ptr - map[i], sy = i;
  }
  que.push(P(sx, sy));
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j < H; j++) {
      for (int k = 0; k < W; k++) {
        vst[j][k] = INF;
      }
    }
    vst[sy][sx] = 0;
    while (!que.empty()) {
      P p = que.front();
      que.pop();
      if (map[p.second][p.first] == i + '0') {
        sum += vst[p.second][p.first];
        while (!que.empty())
          que.pop();
        que.push(P(p.first, p.second));
        sx = p.first, sy = p.second;
        break;
      }
      for (int j = 0; j < 4; j++) {
        int nx = p.first + dx[j], ny = p.second + dy[j];
        if (0 <= nx && nx < W && 0 <= ny && ny < H && map[ny][nx] != 'X' &&
            vst[ny][nx] == INF) {
          vst[ny][nx] = vst[p.second][p.first] + 1;
          que.push(P(nx, ny));
        }
      }
    }
  }
  printf("%d\\n", sum);
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>
#include <queue>
#include <utility>
using namespace std;

#define INF 10000000

typedef pair<int, int> P;

queue<P> que;
char map[1000][1001];
int vst[1000][1000];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int H, W, N, sx, sy, sum = 0;

int main() {
  scanf("%d %d %d", &H, &W, &N);
  for (int i = 0; i < H; i++) {
    scanf("%s", map[i]);
    char *ptr = strchr(map[i], 'S');
    if (ptr != NULL)
      sx = ptr - map[i], sy = i;
  }
  que.push(P(sx, sy));
  for (int i = 1; i <= N; i++) {
    for (int j = 0; j < H; j++) {
      for (int k = 0; k < W; k++) {
        vst[j][k] = INF;
      }
    }
    vst[sy][sx] = 0;
    while (!que.empty()) {
      P p = que.front();
      que.pop();
      if (map[p.second][p.first] == i + '0') {
        sum += vst[p.second][p.first];
        while (!que.empty())
          que.pop();
        que.push(P(p.first, p.second));
        sx = p.first, sy = p.second;
        break;
      }
      for (int j = 0; j < 4; j++) {
        int nx = p.first + dx[j], ny = p.second + dy[j];
        if (map[ny][nx] != 'X' && vst[ny][nx] == INF) {
          vst[ny][nx] = vst[p.second][p.first] + 1;
          que.push(P(nx, ny));
        }
      }
    }
  }
  printf("%d\\n", sum);
  return 0;
}