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

void solve() {
    int n;
    string s;
    cin>>n;
    cin>>s;
    vector<char> b;
    string news = s;
    if(n%4!=0) {
        cout<<"===";
        return;
    }
    map<char, int> m;
    for(char c: s) {
        m[c]++;
    }
    for(auto it = m.begin(); it != m.end(); it++) {
        char c = (*it).first;
        int v = (*it).second;
        if(c != '?') {
            if(v > n/4) {
                cout<<"===";
                return;
            } else {
                for(int i=0; i<((n/4) - v); i++) {
                    b.push_back(c);
                }
            }
        }
    }
    int p=0;
    for(int i=0; i<s.size(); i++) {
        if(s[i]=='?') {
            news[i] = b[p];
            p++;
        }
    }
    cout<<news;
}

int main() {
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    long long t=1;
    //cin>>t;
    while(t--) {
        solve();
    }
    return 0;
}


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

void solve() {
    int n;
    string s;
    cin>>n;
    cin>>s;
    vector<char> b;
    string news = s;
    if(n%4!=0) {
        cout<<"===";
        return;
    }
    map<char, int> m;
    for(char c: s) {
        m[c]++;
    }
        for(auto it = m.begin(); it != m.end(); it++) {
            char c = (*it).first;
            int v = (*it).second;
            if(c != '?') {
                if(v > n/4) {
                    cout<<"===";
                    return;
                } else {
                    for(int i=0; i<((n/4) - v); i++) {
                        b.push_back(c);
                    }
                }
            }
        }
    if(m['A']==0) {
        for(int i=0; i<n/4; i++) {
            b.push_back('A');
        }
    }
    if(m['G']==0) {
        for(int i=0; i<n/4; i++) {
            b.push_back('G');
        }
    }
    if(m['C']==0) {
        for(int i=0; i<n/4; i++) {
            b.push_back('C');
        }
    }
    if(m['T']==0) {
        for(int i=0; i<n/4; i++) {
            b.push_back('T');
        }
    }
    int p=0;
    for(int i=0; i<s.size(); i++) {
        if(s[i]=='?') {
            news[i] = b[p];
            p++;
        }
    }
    cout<<news;
}

int main() {
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    long long t=1;
    //cin>>t;
    while(t--) {
        solve();
    }
    return 0;
}


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

const int maxn = 300+10;
char s[maxn];
int a[maxn];

int main(){
	int n,cnt = 0, a=0,c=0,g=0,t=0;
	scanf("%d%s",&n,&s);
	for(int i=0; i<n; i++){
		if(s[i]=='?') {
			cnt++;
			continue;
		}
		if(s[i]=='A') a++;
		else if(s[i]=='C') c++;
		else if(s[i]=='G') g++;
		else if(s[i]=='T') t++;
	}

	int mx = max(max(max(a,c),g),t);

	int nd = (mx-a)+(mx-c)+(mx-g)+(mx-t);

	if(cnt < nd || n%4!=0) cout << "===\n";
	else{
		for(int i=0; i<n; i++){
			if(s[i]=='?'){
				if(a!=mx){
					a++;
					s[i] = 'A';
				}else if(c!=mx){
					c++;
					s[i] = 'C';
				}else if(g!=mx){
					g++;
					s[i] = 'G';
				}else if(t!=mx){
					t++;
					s[i] = 'T';
				}
			}
		}
		printf("%s\n",s);
	}

}


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

const int maxn = 300+10;
char s[maxn];

int main(){
	int n,cnt = 0, a=0,c=0,g=0,t=0;
	scanf("%d%s",&n,&s);
	for(int i=0; i<n; i++){
		if(s[i]=='?') {
			cnt++;
			continue;
		}
		if(s[i]=='A') a++;
		else if(s[i]=='C') c++;
		else if(s[i]=='G') g++;
		else if(s[i]=='T') t++;
	}

	int mx = max(max(max(a,c),g),t) + cnt/4;

	int nd = (mx-a)+(mx-c)+(mx-g)+(mx-t);

	cout << "mx" << mx << " nd" << nd << " cnt" << cnt << endl;

	if(cnt != nd || n%4!=0) cout << "===\n";
	else{
		for(int i=0; i<n; i++){
			if(s[i]=='?'){
				if(a<mx){
					a++;
					s[i] = 'A';
				}else if(c<mx){
					c++;
					s[i] = 'C';
				}else if(g<mx){
					g++;
					s[i] = 'G';
				}else if(t<mx){
					t++;
					s[i] = 'T';
				}
			}
		}
		printf("%s\n",s);
	}

}


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

const int maxn = 300+10;
char s[maxn];

int main(){
	int n,cnt = 0, a=0,c=0,g=0,t=0;
	scanf("%d%s",&n,&s);
	for(int i=0; i<n; i++){
		if(s[i]=='?') {
			cnt++;
			continue;
		}
		if(s[i]=='A') a++;
		else if(s[i]=='C') c++;
		else if(s[i]=='G') g++;
		else if(s[i]=='T') t++;
	}

	int mx = max(max(max(a,c),g),t) + cnt/4;

	int nd = (mx-a)+(mx-c)+(mx-g)+(mx-t);

	// cout << "mx" << mx << " nd" << nd << " cnt" << cnt << endl;

	if(cnt != nd || n%4!=0) cout << "===\n";
	else{
		for(int i=0; i<n; i++){
			if(s[i]=='?'){
				if(a<mx){
					a++;
					s[i] = 'A';
				}else if(c<mx){
					c++;
					s[i] = 'C';
				}else if(g<mx){
					g++;
					s[i] = 'G';
				}else if(t<mx){
					t++;
					s[i] = 'T';
				}
			}
		}
		printf("%s\n",s);
	}

}


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

const int maxn = 300+10;
char s[maxn];
int a[maxn];

int main(){
	int n,cnt = 0, a=0,c=0,g=0,t=0;
	scanf("%d%s",&n,&s);
	for(int i=0; i<n; i++){
		if(s[i]=='?') {
			cnt++;
			continue;
		}
		if(s[i]=='A') a++;
		else if(s[i]=='C') c++;
		else if(s[i]=='G') g++;
		else if(s[i]=='T') t++;
	}

	int mx = max(max(max(a,c),g),t);

	int nd = (mx-a)+(mx-c)+(mx-g)+(mx-t);

	if(nd == 0) {
		mx = n/4;
	}

	if(cnt < nd || n%4!=0) cout << "===\n";
	else{
		for(int i=0; i<n; i++){
			if(s[i]=='?'){
				if(a!=mx){
					a++;
					s[i] = 'A';
				}else if(c!=mx){
					c++;
					s[i] = 'C';
				}else if(g!=mx){
					g++;
					s[i] = 'G';
				}else if(t!=mx){
					t++;
					s[i] = 'T';
				}
			}
		}
		printf("%s\n",s);
	}

}


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

const int maxn = 300+10;
char s[maxn];

int main(){
	int n,cnt = 0, a=0,c=0,g=0,t=0;
	scanf("%d%s",&n,&s);
	for(int i=0; i<n; i++){
		if(s[i]=='?') {
			cnt++;
			continue;
		}
		if(s[i]=='A') a++;
		else if(s[i]=='C') c++;
		else if(s[i]=='G') g++;
		else if(s[i]=='T') t++;
	}

	int mx = n/4;
	if(mx < a || mx < g || mx < c || mx < t) {
		cout << "===\n";
		return 0;
	}
	int nd = (mx-a)+(mx-c)+(mx-g)+(mx-t);

	// cout << "mx" << mx << " nd" << nd << " cnt" << cnt << endl;

	if(cnt != nd || n%4!=0) cout << "===\n";
	else{
		for(int i=0; i<n; i++){
			if(s[i]=='?'){
				if(a<mx){
					a++;
					s[i] = 'A';
				}else if(c<mx){
					c++;
					s[i] = 'C';
				}else if(g<mx){
					g++;
					s[i] = 'G';
				}else if(t<mx){
					t++;
					s[i] = 'T';
				}
			}
		}
		printf("%s\n",s);
	}

}


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

const int maxn = 300+10;
char s[maxn];
int a[maxn];

int main(){
	int n,cnt = 0, a=0,c=0,g=0,t=0;
	scanf("%d%s",&n,&s);
	for(int i=0; i<n; i++){
		if(s[i]=='?') {
			cnt++;
			continue;
		}
		if(s[i]=='A') a++;
		else if(s[i]=='C') c++;
		else if(s[i]=='G') g++;
		else if(s[i]=='T') t++;
	}

	int mx = max(max(max(a,c),g),t) + cnt/4;

	int nd = (mx-a)+(mx-c)+(mx-g)+(mx-t);

	if(cnt != nd || n%4!=0) cout << "===\n";
	else{
		for(int i=0; i<n; i++){
			if(s[i]=='?'){
				if(a!=mx){
					a++;
					s[i] = 'A';
				}else if(c!=mx){
					c++;
					s[i] = 'C';
				}else if(g!=mx){
					g++;
					s[i] = 'G';
				}else if(t!=mx){
					t++;
					s[i] = 'T';
				}
			}
		}
		printf("%s\n",s);
	}

}


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

const int maxn = 300+10;
char s[maxn];
int a[maxn];

int main(){
	int n,cnt = 0, a=0,c=0,g=0,t=0;
	scanf("%d%s",&n,&s);
	for(int i=0; i<n; i++){
		if(s[i]=='?') {
			cnt++;
			continue;
		}
		if(s[i]=='A') a++;
		else if(s[i]=='C') c++;
		else if(s[i]=='G') g++;
		else if(s[i]=='T') t++;
	}

	int mx = max(max(max(a,c),g),t) + cnt/4;

	int nd = (mx-a)+(mx-c)+(mx-g)+(mx-t) ;

	if(cnt < nd || n%4!=0) cout << "===\n";
	else{
		for(int i=0; i<n; i++){
			if(s[i]=='?'){
				if(a!=mx){
					a++;
					s[i] = 'A';
				}else if(c!=mx){
					c++;
					s[i] = 'C';
				}else if(g!=mx){
					g++;
					s[i] = 'G';
				}else if(t!=mx){
					t++;
					s[i] = 'T';
				}
			}
		}
		printf("%s\n",s);
	}

}


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

const int maxn = 300+10;
char s[maxn];

int main(){
	int n,cnt = 0, a=0,c=0,g=0,t=0;
	scanf("%d%s",&n,&s);
	for(int i=0; i<n; i++){
		if(s[i]=='?') {
			cnt++;
			continue;
		}
		if(s[i]=='A') a++;
		else if(s[i]=='C') c++;
		else if(s[i]=='G') g++;
		else if(s[i]=='T') t++;
	}

	int mx = n/4;
	int nd = (mx-a)+(mx-c)+(mx-g)+(mx-t);

	// cout << "mx" << mx << " nd" << nd << " cnt" << cnt << endl;

	if(cnt != nd || n%4!=0) cout << "===\n";
	else{
		for(int i=0; i<n; i++){
			if(s[i]=='?'){
				if(a<mx){
					a++;
					s[i] = 'A';
				}else if(c<mx){
					c++;
					s[i] = 'C';
				}else if(g<mx){
					g++;
					s[i] = 'G';
				}else if(t<mx){
					t++;
					s[i] = 'T';
				}
			}
		}
		printf("%s\n",s);
	}

}


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

bool comp(pair<char, int> p1, pair<char, int> p2){
    return p1.second<p2.second;
}

int main(){
    int t;
    string s;
    cin>>t;
    cin>>s;
    unordered_map<char, int> M = {{'A', 0}, {'C', 0}, {'G', 0}, {'T', 0}};
    int q = 0;
    for(auto c:s){
        if(c=='?'){
            q++;
        }
        else{
            M[c]++;
        }
    }
    auto p = max_element(M.begin(), M.end(), comp);
    int mxx = p->second, req=0;
    queue<char> Q;
    for(auto p:M){
        M[p.first] = mxx-p.second;
        while(M[p.first]--){
            Q.push(p.first);
        }
    }
    int diff = (q - Q.size());
    if((diff%4!=0) || diff<0){
        cout<<"===";
    }
    else{
        for(int i=0; i<diff/4; i++){
            Q.push('A');
            Q.push('C');
            Q.push('G');
            Q.push('T');
        }
        for(int i=0; i<t; i++){
            if(s[i] == '?'){
                s[i] = Q.front();
                Q.pop();
            }
        }
        cout<<s;
    }
    cout<<"\n";
    return 0;
}


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

bool comp(pair<char, int> p1, pair<char, int> p2){
    return p1.second<p2.second;
}

int main(){
    int t;
    string s;
    cin>>t;
    cin>>s;
    unordered_map<char, int> M = {{'A', 0}, {'C', 0}, {'G', 0}, {'T', 0}};
    int q = 0;
    for(auto c:s){
        if(c=='?'){
            q++;
        }
        else{
            M[c]++;
        }
    }
    auto p = max_element(M.begin(), M.end(), comp);
    int mxx = p->second, req=0;
    queue<char> Q;
    for(auto p:M){
        M[p.first] = mxx-p.second;
        while(M[p.first]--){
            Q.push(p.first);
        }
    }
    int diff = (q - Q.size());
    if(diff%4!=0){
        cout<<"===";
    }
    else{
        for(int i=0; i<diff/4; i++){
            Q.push('A');
            Q.push('C');
            Q.push('G');
            Q.push('T');
        }
        for(int i=0; i<t; i++){
            if(s[i] == '?'){
                s[i] = Q.front();
                Q.pop();
            }
        }
        cout<<s;
    }
    cout<<"\n";
    return 0;
}


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

bool comp(pair<char, int> p1, pair<char, int> p2){
    return p1.second<p2.second;
}

int main(){
    int t;
    string s;
    cin>>t;
    cin>>s;
    unordered_map<char, int> M;
    int q = 0;
    for(auto c:s){
        if(c=='?'){
            q++;
        }
        else{
            M[c]++;
        }
    }
    auto p = max_element(M.begin(), M.end(), comp);
    int mxx = p->second, req=0;
    queue<char> Q;
    for(auto p:M){
        M[p.first] = mxx-p.second;
        while(M[p.first]--){
            Q.push(p.first);
        }
    }
    if(Q.size()!=q){
        cout<<"===";
    }
    else{
        for(int i=0; i<t; i++){
            if(s[i] == '?'){
                s[i] = Q.front();
                Q.pop();
            }
        }
        cout<<s;
    }
    cout<<"\n";
    return 0;
}


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

bool comp(pair<char, int> p1, pair<char, int> p2){
    return p1.second<p2.second;
}

int main(){
    int t;
    string s;
    cin>>t;
    cin>>s;
    unordered_map<char, int> M;
    int q = 0;
    for(auto c:s){
        if(c=='?'){
            q++;
        }
        else{
            M[c]++;
        }
    }
    auto p = max_element(M.begin(), M.end(), comp);
    int mxx = p->second, req=0;
    for(auto p:M){
        req += mxx-p.second;
    }
    if(req!=q){
        cout<<"===";
    }
    else{
        for(int i=0; i<t; i++){
            if(s[i]=='?'){
                s[i] = M.begin()->first;
                M.begin()->second--;
                if(M.begin()->second<=0){
                    M.erase(M.begin());
                }
            }
        }
        cout<<s;
    }
    cout<<"\n";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n,l,i,a=0,c=0,g=0,t=0,e=0,z,z1,p;
    string q;
    cin>>n>>q;
    l=q.length();
    for(i=0;i<l;i++)
    { if(q[i]=='A')
         a++;
      if(q[i]=='C')
        c++;
      if(q[i]=='G')
        g++;
      if(q[i]=='T')
        t++;
      if(q[i]=='?')
        e++;
    }
    if(a==c==g==e)
        cout<<q;
    else{
     z=max(a,c);
     z1=max(z,g);
     p=max(z1,t);
    for(i=0;i<l;i++){
        if(q[i]=='?')
    { if(a<p)
         {q[i]='A';
         a++;
          continue;
          }
       if(c<p)
        {q[i]='C';
        c++;
        continue;
        }
       if(g<p)
       {q[i]='G';
       g++;
        continue;
        }
       if(t<p)
        {q[i]='T';
        t++;
        continue;

        }

    }}
    cout<<q;

    }

    return 0;
}


#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <cmath>
#include <iostream>
#define LL long long
using namespace std;
int data[30];
char pos[1000000];
int main(){
    int n;
    scanf("%d", &n);
    scanf("%s", pos);
    int ss=0;
    for(int a=0; a<n; a++){
        if(pos[a]=='?')ss++;
        else data[pos[a]-'A']++;
    }
    int Max=-1;
    Max=max(data['A'-'A'], Max);
    Max=max(data['C'-'A'], Max);
    Max=max(data['G'-'A'], Max);
    Max=max(data['T'-'A'], Max);
    int now=0;
    now+=Max-data['A'-'A'];
    now+=Max-data['C'-'A'];
    now+=Max-data['G'-'A'];
    now+=Max-data['T'-'A'];
    if(ss>=now&&(ss-now)%4==0){
        int sss=Max+(ss-now)/4;
        for(int a=0; a<n; a++){
            if(pos[a]!='?')printf("%c", pos[a]);
            else {
                if(data['A'-'A']<sss){
                    printf("A");
                    data['A'-'A']++;
                }
                else if(data['C'-'A']<sss){
                    printf("C");
                    data['C'-'A']++;
                }
                else if(data['G'-'A']<sss){
                    printf("G");
                    data['G'-'A']++;
                }
                else if(data['T'-'A']<sss){
                    printf("T");
                    data['T'-'A']++;
                }
            }
        }
    }
    else printf("===");
    printf("\n");
    return 0;
}


#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <cmath>
#include <iostream>
#define LL long long
using namespace std;
int data[30];
char pos[1000000];
int main(){
    int n;
    scanf("%d", &n);
    scanf("%s", pos);
    int ss=0;
    for(int a=0; a<n; a++){
        if(pos[a]=='?')ss++;
        else data[pos[a]-'A']++;
    }
    int Max=-1;
    Max=max(data['A'-'A'], Max);
    Max=max(data['C'-'A'], Max);
    Max=max(data['G'-'A'], Max);
    Max=max(data['T'-'A'], Max);
    int now=0;
    now+=Max-data['A'-'A'];
    now+=Max-data['C'-'A'];
    now+=Max-data['G'-'A'];
    now+=Max-data['T'-'A'];
    if(ss>=now&&(ss-now)%4==0){
        int sss=Max+(ss-now)/4;
        for(int a=0; a<n; a++){
            if(pos[a]!='?')printf("%c", pos[a]);
            else {
                if(data['A'-'A']<Max){
                    printf("A");
                    data['A'-'A']++;
                }
                else if(data['C'-'A']<Max){
                    printf("C");
                    data['C'-'A']++;
                }
                else if(data['G'-'A']<Max){
                    printf("G");
                    data['G'-'A']++;
                }
                else if(data['T'-'A']<Max){
                    printf("T");
                    data['T'-'A']++;
                }
            }
        }
    }
    else printf("===");
    printf("\n");
    return 0;
}


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

int main() {
    int n;
    string str;
    cin >> n;
    cin >> str;
    if (n % 4 != 0) {
        printf("===");
        return 0;
    }

    int A, G, C, T;
    A = G = C = T = 0;
    for (int i = 0; i < str.size(); i++) {
        if (str[i] == 'A') A++;
        if (str[i] == 'G') G++;
        if (str[i] == 'C') C++;
        if (str[i] == 'T') T++;
    }
    if (A > n / 4 || G > n / 4 || C > n / 4 || T > n / 4) {
        printf("===");
        return 0;
    }

    A = n / 4 - A;
    G = n / 4 - G;
    C = n / 4 - C;
    T = n / 4 - T;
    for (int i = 0; i < str.size(); i++) {
        if (str[i] != '?') {
            cout << str[i];
            continue;
        }
        if (A) {
            cout << "A";
            A--;
        } else if (G) {
            cout << "G";
            G--;
        } else if (C) {
            cout << "C";
            C--;
        } else if (T) {
            cout << "T";
            T--;
        }
    }
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;
char s[1010];
int main()
{
    int n,A = 0,G = 0,C = 0,T=0;
    scanf("%d %s",&n,s);
    for(int i = 0 ; i < n; i++){
        if(s[i] == '?');
        else if(s[i] == 'A') A++;
        else if(s[i] == 'T') T++;
        else if(s[i] == 'C') C++;
        else if(s[i] == 'G') G++;
    }
    int q = n / 4;
    if(n % 4 || A > q || T > q || C > q || G > q){
        printf("===\n");
    }
    else{
        for(int i = 0 ; i < n; i++)
            if(s[i] == '?'){
                if(A < q) s[i] = 'A',A++;
                else if(T < q) s[i] = 'T',T++;
                else if(C < q) s[i] = 'C',C++;
                else if(G < q) s[i] = 'G',G++;
            }
        printf("%s\n",s);
    }
    return 0;
}


#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <cmath>
#include <iostream>
#define LL long long
using namespace std;
int data[30];
char pos[1000000];
int main(){
    int n;
    scanf("%d", &n);
    scanf("%s", pos);
    int ss=0;
    for(int a=0; a<n; a++){
        if(pos[a]=='?')ss++;
        else data[pos[a]-'A']++;
    }
    int Max=-1;
    Max=max(data['A'-'A'], Max);
    Max=max(data['C'-'A'], Max);
    Max=max(data['G'-'A'], Max);
    Max=max(data['T'-'A'], Max);
    int now=0;
    now+=Max-data['A'-'A'];
    now+=Max-data['C'-'A'];
    now+=Max-data['G'-'A'];
    now+=Max-data['T'-'A'];
    if((ss-now)%4==0){
        int sss=Max+(ss-now)/4;
        for(int a=0; a<n; a++){
            if(pos[a]!='?')printf("%c", pos[a]);
            else {
                if(data['A'-'A']<Max){
                    printf("A");
                    data['A'-'A']++;
                }
                else if(data['C'-'A']<Max){
                    printf("C");
                    data['C'-'A']++;
                }
                else if(data['G'-'A']<Max){
                    printf("G");
                    data['G'-'A']++;
                }
                else if(data['T'-'A']<Max){
                    printf("T");
                    data['T'-'A']++;
                }
            }
        }
    }
    else printf("===");
    printf("\n");
    return 0;
}


#include <bits/stdc++.h>
typedef long long ll;
const int mod = 1e9 + 7;
const int MAX = 1e5 + 7;
using namespace std;
typedef vector<int> vi;
class MammothGenomeEncoding
{
public:
	void solve() {
		int n; string s;
		cin >> n >> s;
		int a = 0, g = 0, c = 0, t = 0, q = 0;
		for (int i = 0; i < n; i++) {
			switch (s[i]) {
			case 'A': a++; break;
			case 'G': g++; break;
			case 'C': c++; break;
			case 'T': t++; break;
			case '?': q++; break;
			}
		}
		int tot = max(max(a, g), max(c, t));
		int na = tot - a;
		int ng = tot - g;
		int nc = tot - c;
		int nt = tot - t;
		if (q != (na + ng + nc + nt)) {
			cout << "==="; return;
		}

		for (int i = 0; i < n; i++) {
			if (s[i] == '?') {
				if (na) {
					s[i] = 'A'; na--;
				} else if (ng) {
					s[i] = 'G'; ng--;
				} else if (nc) {
					s[i] = 'C'; nc--;
				} else if (nt) {
					s[i] = 'T'; nt--;
				}
			}
		}
		cout << s;
	}

};
int main() {
#ifndef ONLINE_JUDGE
	freopen("/Users/seeva92/Workspace/Contests/1.txt", "r", stdin);
	freopen("/Users/seeva92/Workspace/Contests/2.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0);
	MammothGenomeEncoding m; m.solve();
}


#include <bits/stdc++.h>
typedef long long ll;
const int mod = 1e9 + 7;
const int MAX = 1e5 + 7;
using namespace std;
typedef vector<int> vi;
class MammothGenomeEncoding
{
public:
	void solve() {
		int n; string s;
		cin >> n >> s;
		int a = 0, g = 0, c = 0, t = 0, q = 0;
		for (int i = 0; i < n; i++) {
			switch (s[i]) {
			case 'A': a++; break;
			case 'G': g++; break;
			case 'C': c++; break;
			case 'T': t++; break;
			case '?': q++; break;
			}
		}
		int tot = max(max(a, g), max(c, t));
		int na = tot - a;
		int ng = tot - g;
		int nc = tot - c;
		int nt = tot - t;

		if ((q == n) && (n % 4 == 0)) {
			na = ng = nc = nt = n / 4;
		}

		if (q != (na + ng + nc + nt)) {
			cout << "==="; return;
		}

		for (int i = 0; i < n; i++) {
			if (s[i] == '?') {
				if (na) {
					s[i] = 'A'; na--;
				} else if (ng) {
					s[i] = 'G'; ng--;
				} else if (nc) {
					s[i] = 'C'; nc--;
				} else if (nt) {
					s[i] = 'T'; nt--;
				}
			}
		}
		cout << s;
	}

};
int main() {
#ifndef ONLINE_JUDGE
	freopen("/Users/seeva92/Workspace/Contests/1.txt", "r", stdin);
	freopen("/Users/seeva92/Workspace/Contests/2.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0);
	MammothGenomeEncoding m; m.solve();
}


#include <bits/stdc++.h>
typedef long long ll;
const int mod = 1e9 + 7;
const int MAX = 1e5 + 7;
using namespace std;
typedef vector<int> vi;
class MammothGenomeEncoding
{
	int a = 0, g = 0, c = 0, t = 0, q = 0;
	int n; string s;

public:
	void check() {
		a = g = c = t = q = 0;
		for (int i = 0; i < n; i++) {
			switch (s[i]) {
			case 'A': a++; break;
			case 'G': g++; break;
			case 'C': c++; break;
			case 'T': t++; break;
			case '?': q++; break;
			}
		}
	}
	void solve() {
		cin >> n >> s;
		check();
		int tot = max(max(a, g), max(c, t));
		int na = tot - a;
		int ng = tot - g;
		int nc = tot - c;
		int nt = tot - t;
		if ((q == n) && ((n % 4) == 0)) {
			na = ng = nc = nt = n / 4;
		}
		for (int i = 0; i < n; i++) {
			if (s[i] == '?') {
				if (na) {
					s[i] = 'A'; na--;
				} else if (ng) {
					s[i] = 'G'; ng--;
				} else if (nc) {
					s[i] = 'C'; nc--;
				} else if (nt) {
					s[i] = 'T'; nt--;
				}
			}
		}
		check();
		if ((a == g) && (a == c) && (a == t) && (q == 0)) {
			cout << s;
		} else {
			cout << "===";
		}
	}

};
int main() {
#ifndef ONLINE_JUDGE
	freopen("/Users/seeva92/Workspace/Contests/1.txt", "r", stdin);
	freopen("/Users/seeva92/Workspace/Contests/2.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0);
	MammothGenomeEncoding m; m.solve();
}


#include <bits/stdc++.h>
typedef long long ll;
const int mod = 1e9 + 7;
const int MAX = 1e5 + 7;
using namespace std;
typedef vector<int> vi;
class MammothGenomeEncoding
{
	int a = 0, g = 0, c = 0, t = 0, q = 0;
	int n; string s;

public:
	void check() {
		a = g = c = t = q = 0;
		for (int i = 0; i < n; i++) {
			switch (s[i]) {
			case 'A': a++; break;
			case 'G': g++; break;
			case 'C': c++; break;
			case 'T': t++; break;
			case '?': q++; break;
			}
		}
	}
	void solve() {
		cin >> n >> s;
		check();
		int tot = max(max(a, g), max(c, t));
		int na = tot - a;
		int ng = tot - g;
		int nc = tot - c;
		int nt = tot - t;
		if ((q - (na + ng + nc + nt)) % 4 == 0) {
			int temp = (q - (na + ng + nc + nt));
			na += (temp / 4);
			ng += (temp / 4);
			nc += (temp / 4);
			nt += (temp / 4);
			for (int i = 0; i < n; i++) {
				if (s[i] == '?') {
					if (na) {
						s[i] = 'A'; na--;
					} else if (ng) {
						s[i] = 'G'; ng--;
					} else if (nc) {
						s[i] = 'C'; nc--;
					} else if (nt) {
						s[i] = 'T'; nt--;
					}
				}
			}
			cout << s;
		} else {
			cout << "===";
		}
	}

};
int main() {
#ifndef ONLINE_JUDGE
	freopen("/Users/seeva92/Workspace/Contests/1.txt", "r", stdin);
	freopen("/Users/seeva92/Workspace/Contests/2.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0);
	MammothGenomeEncoding m; m.solve();
}


#include <bits/stdc++.h>
typedef long long ll;
const int mod = 1e9 + 7;
const int MAX = 1e5 + 7;
using namespace std;
typedef vector<int> vi;
class MammothGenomeEncoding
{
	int a = 0, g = 0, c = 0, t = 0, q = 0;
	int n; string s;

public:
	void check() {
		a = g = c = t = q = 0;
		for (int i = 0; i < n; i++) {
			switch (s[i]) {
			case 'A': a++; break;
			case 'G': g++; break;
			case 'C': c++; break;
			case 'T': t++; break;
			case '?': q++; break;
			}
		}
	}
	void solve() {
		cin >> n >> s;
		check();
		int tot = max(max(a, g), max(c, t));
		int na = tot - a;
		int ng = tot - g;
		int nc = tot - c;
		int nt = tot - t;
		if (q < (na + ng + nc + nt)) {
			cout << "===";
		}
		else if ((q - (na + ng + nc + nt)) % 4 == 0) {
			int temp = (q - (na + ng + nc + nt));
			na += (temp / 4);
			ng += (temp / 4);
			nc += (temp / 4);
			nt += (temp / 4);
			for (int i = 0; i < n; i++) {
				if (s[i] == '?') {
					if (na) {
						s[i] = 'A'; na--;
					} else if (ng) {
						s[i] = 'G'; ng--;
					} else if (nc) {
						s[i] = 'C'; nc--;
					} else if (nt) {
						s[i] = 'T'; nt--;
					}
				}
			}
			cout << s;
		} else {
			cout << "===";
		}
	}

};
int main() {
#ifndef ONLINE_JUDGE
	freopen("/Users/seeva92/Workspace/Contests/1.txt", "r", stdin);
	freopen("/Users/seeva92/Workspace/Contests/2.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0);
	MammothGenomeEncoding m; m.solve();
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i = (a); i < (b); i++)
typedef pair<int,int> ii;

int main() {

	map<char,int> l;
	int n;
	cin >> n;
	if(n % 4 != 0) {
		cout << "===" << endl;
		return 0;
	}
	int want = n/4;
	l['A'] = want;
	l['C'] = want;
	l['G'] = want;
	l['T'] = want;
	string j;
	cin >> j;
	rep(i,0,j.size()) {
		if(j[i] != '?') {
			l[j[i]]--;
		}
	}
	vector<char> need;
	for(map<char,int>::iterator it = l.begin(); it != l.end(); it++){
		if(it->second < 0) {
			cout << "===" << endl;
			return 0;
		}
		rep(i,0,it->second) {
			need.push_back(it->first);
		}
	}
	string ne = "";
	rep(i,0,j.size()) {
		if(j[i] != '?') ne += j[i];
		else {
			ne += need[0];
			need.erase(need.begin());
		}
	}
	cout << ne << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i = (a); i < (b); i++)
typedef pair<int,int> ii;

int main() {

	map<char,int> l;
	l['A'] = 0;
	l['G'] = 0;
	l['C'] = 0;
	l['T'] = 0;
	string j;
	int n;
	cin >> n;
	cin >> j;
	int hi = 0;
	rep(i,0,j.size()) {
		if(j[i] != '?') {
			l[j[i]]++;
			if(l[j[i]] > hi) hi = l[j[i]];
		}
	}
	string ne = "";
	vector<char> m;
	for(map<char,int>::iterator it = l.begin(); it != l.end(); it++) {
		int k = hi-it->second;
		rep(i,0,k) {
			m.push_back(it->first);
		}
	}
	bool can = true;
	rep(i,0,j.size()) {
		if(j[i] != '?') ne += j[i];
		else {
			if(m.size() != 0) {
				ne += m[0];
				m.erase(m.begin());
			} else {
				can = false;
			}
		}
	}
	if(can && m.size() == 0) {
		cout << ne << endl;
	} else cout << "===" << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i = (a); i < (b); i++)
typedef pair<int,int> ii;

int main() {

	map<char,int> l;
	l['A'] = 0;
	l['G'] = 0;
	l['C'] = 0;
	l['T'] = 0;
	string j;
	int n;
	cin >> n;
	cin >> j;
	int hi = 0;
	rep(i,0,j.size()) {
		if(j[i] != '?') {
			l[j[i]]++;
			if(l[j[i]] > hi) hi = l[j[i]];
		}
	}
	bool can = true;
	if(hi == 0) {
		if(n % 4 == 0) hi = n / 4;
		else can = false;
	}
	string ne = "";
	vector<char> m;
	for(map<char,int>::iterator it = l.begin(); it != l.end(); it++) {
		int k = hi-it->second;
		rep(i,0,k) {
			m.push_back(it->first);
		}
	}
	rep(i,0,j.size()) {
		if(j[i] != '?') ne += j[i];
		else {
			if(m.size() != 0) {
				ne += m[0];
				m.erase(m.begin());
			} else {
				can = false;
			}
		}
	}
	if(n % 4 != 0) {
		cout << "===" << endl;
		return 0;
	}
	if(can && m.size() == 0) {
		cout << ne << endl;
	} else cout << "===" << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i = (a); i < (b); i++)
typedef pair<int,int> ii;

int main() {

	map<char,int> l;
	l['A'] = 0;
	l['G'] = 0;
	l['C'] = 0;
	l['T'] = 0;
	string j;
	int n;
	cin >> n;
	cin >> j;
	int hi = 0;
	bool has = false;
	rep(i,0,j.size()) {
		if(j[i] != '?') {
			l[j[i]]++;
			if(l[j[i]] > hi) hi = l[j[i]];
		} else has = true;
	}
	if(!has) {
		cout << j << endl;
		return 0;
	}
	bool can = true;
	if(hi == 0) {
		if(n % 4 == 0) hi = n / 4;
		else can = false;
	}
	string ne = "";
	vector<char> m;
	for(map<char,int>::iterator it = l.begin(); it != l.end(); it++) {
		int k = hi-it->second;
		rep(i,0,k) {
			m.push_back(it->first);
		}
	}
	rep(i,0,j.size()) {
		if(j[i] != '?') ne += j[i];
		else {
			if(m.size() != 0) {
				ne += m[0];
				m.erase(m.begin());
			} else {
				can = false;
			}
		}
	}
	if(n % 4 != 0) {
		cout << "===" << endl;
		return 0;
	}
	if(can && m.size() == 0) {
		cout << ne << endl;
	} else cout << "===" << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i = (a); i < (b); i++)
typedef pair<int,int> ii;

int main() {

	map<char,int> l;
	l['A'] = 0;
	l['G'] = 0;
	l['C'] = 0;
	l['T'] = 0;
	string j;
	int n;
	cin >> n;
	cin >> j;
	int hi = 0;
	rep(i,0,j.size()) {
		if(j[i] != '?') {
			l[j[i]]++;
			if(l[j[i]] > hi) hi = l[j[i]];
		}
	}
	bool can = true;
	if(hi == 0) {
		if(n % 4 == 0) hi = n / 4;
		else can = false;
	}
	string ne = "";
	vector<char> m;
	for(map<char,int>::iterator it = l.begin(); it != l.end(); it++) {
		int k = hi-it->second;
		rep(i,0,k) {
			m.push_back(it->first);
		}
	}
	rep(i,0,j.size()) {
		if(j[i] != '?') ne += j[i];
		else {
			if(m.size() != 0) {
				ne += m[0];
				m.erase(m.begin());
			} else {
				can = false;
			}
		}
	}
	if(can && m.size() == 0) {
		cout << ne << endl;
	} else cout << "===" << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i = (a); i < (b); i++)
typedef pair<int,int> ii;

int main() {

	int n;
	cin >> n;
	int best = 100000;
	int x = 0;
	int y = 0;
	rep(i,1,n) {
		if(n % i == 0) {
			int f = n/i;
			if(abs(i-f) < best) {
				best = abs(i-f);
				x = i;
				y = f;
			}
		}
	}
	cout << x << " " << y << endl;
	return 0;
}


#pragma GCC optimize(2)
#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define rep(i,n) for(int i=1;i<=(n);i++)
#define rep0(i,n) for(int i=0;i<(n);i++)

const ll INF=1e9+7;
const int dx[5]={0,-1,0,1,0};
const int dy[5]={0,0,1,0,-1};
int n,a,b,mas;
char ma[16][16];
int x[10];
int y[10];
bool vis[16][16][1<<16];
struct hhh{
	int xx;
	int yy;
	int mask;
	int step;
};
void bfs(){
	queue<hhh> q;
	hhh w;
	w.xx=x[1];
	w.yy=y[1];
	w.mask=mas;
	w.step=0;
	q.push(w);
	while(!q.empty()){
		hhh t=q.front();
		q.pop();
		if(ma[t.xx][t.yy]=='@'){
			cout<<t.step;
			return;
		}
		vis[t.xx][t.yy][t.mask]=1;
		int xxx=t.xx,yyy=t.yy;
		ma[xxx][yyy]='#';
		for(int l=(n-1)*2-1;l>=3;l-=2){
			bool f1=t.mask&(1<<l),f2=t.mask&(1<<l-1);
			if(!f1&&!f2){
				xxx+=dx[1];
				yyy+=dy[1];
			}
			else if(!f1&&f2){
				xxx+=dx[2];
				yyy+=dy[2];
			}
			else if(f1&&!f2){
				xxx+=dx[3];
				yyy+=dy[3];
			}
			else if(f1&&f2){
				xxx+=dx[4];
				yyy+=dy[4];
			}
			ma[xxx][yyy]='#';
		}
		rep(i,4){
			int dxx=t.xx+dx[i];
			int dyy=t.yy+dy[i];
			if(dxx>=1&&dxx<=a&&dyy>=1&&dyy<=b&&ma[dxx][dyy]!='#'){
				int k=t.mask;
				k=k>>2;
				if(i==4){
					k|=(1<<(n-1)*2-2);
				}
				else if(i==1){
					k|=(1<<(n-1)*2-1);
				}
				else if(i==2){
					k|=(1<<(n-1)*2-1)|(1<<(n-1)*2-2);
				}
				if(vis[dxx][dyy][k]){
					continue;
				}
				bool flag=1;
				hhh e;
				e.xx=dxx;
				e.yy=dyy;
				e.mask=k;
				e.step=t.step+1;
				q.push(e);
			}
		}
		xxx=t.xx,yyy=t.yy;
		ma[xxx][yyy]='.';
		for(int l=(n-1)*2-1;l>=3;l-=2){
			bool f1=t.mask&(1<<l),f2=t.mask&(1<<l-1);
			if(!f1&&!f2){
				xxx+=dx[1];
				yyy+=dy[1];
			}
			else if(!f1&&f2){
				xxx+=dx[2];
				yyy+=dy[2];
			}
			else if(f1&&!f2){
				xxx+=dx[3];
				yyy+=dy[3];
			}
			else if(f1&&f2){
				xxx+=dx[4];
				yyy+=dy[4];
			}
			ma[xxx][yyy]='.';
		}
	}
	cout<<-1;
	return;
}
int main(){
	/*freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);*/
	cin>>a>>b;
	rep(i,a){
		rep(j,b){
			cin>>ma[i][j];
			if(ma[i][j]>='1'&&ma[i][j]<='9'){
				n=max(n,ma[i][j]-'0');
				x[ma[i][j]-'0']=i;
				y[ma[i][j]-'0']=j;
			}
		}
	}
	rep(i,n-1){
		rep(j,4){
			if(x[i]+dx[j]==x[i+1]&&y[i]+dy[j]==y[i+1]){
				if(j==2){
					mas|=(1<<(n-i)*2-2);
				}
				else if(j==3){
					mas|=(1<<(n-i)*2-1);
				}
				else if(j==4){
					mas|=(1<<((n-i)*2)-1)|(1<<(n-i)*2-2);
				}
				break;
			}
		}
//		cout<<mas<<endl;
	}
	bfs();
	return 0;
}


#pragma GCC optimize(2)
#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define rep(i,n) for(int i=1;i<=(n);i++)
#define rep0(i,n) for(int i=0;i<(n);i++)

const ll INF=1e9+7;
const int dx[5]={0,-1,0,1,0};
const int dy[5]={0,0,1,0,-1};
int n,a,b,mas;
char ma[16][16];
int x[10];
int y[10];
bool vis[16][16][1<<16];
struct hhh{
	int xx;
	int yy;
	int mask;
	int step;
};
void bfs(){
	queue<hhh> q;
	hhh w;
	w.xx=x[1];
	w.yy=y[1];
	w.mask=mas;
	w.step=0;
	q.push(w);
	while(!q.empty()){
		hhh t=q.front();
		q.pop();
		if(ma[t.xx][t.yy]=='@'){
			cout<<t.step;
			return;
		}
		vis[t.xx][t.yy][t.mask]=1;
		int xxx=t.xx,yyy=t.yy;
		ma[xxx][yyy]='#';
		for(int l=(n-1)*2-1;l>=3;l-=2){
			bool f1=t.mask&(1<<l),f2=t.mask&(1<<l-1);
			if(!f1&&!f2){
				xxx+=dx[1];
				yyy+=dy[1];
			}
			else if(!f1&&f2){
				xxx+=dx[2];
				yyy+=dy[2];
			}
			else if(f1&&!f2){
				xxx+=dx[3];
				yyy+=dy[3];
			}
			else if(f1&&f2){
				xxx+=dx[4];
				yyy+=dy[4];
			}
			ma[xxx][yyy]='#';
		}
		rep(i,4){
			int dxx=t.xx+dx[i];
			int dyy=t.yy+dy[i];
			if(dxx>=1&&dxx<=a&&dyy>=1&&dyy<=b&&ma[dxx][dyy]!='#'){
				int k=t.mask;
				k=k>>2;
				if(i==4){
					k|=(1<<(n-1)*2-2);
				}
				else if(i==1){
					k|=(1<<(n-1)*2-1);
				}
				else if(i==2){
					k|=(1<<(n-1)*2-1)|(1<<(n-1)*2-2);
				}
				if(vis[dxx][dyy][k]){
					continue;
				}
				bool flag=1;
				hhh e;
				e.xx=dxx;
				e.yy=dyy;
				e.mask=k;
				e.step=t.step+1;
				q.push(e);
			}
		}
		xxx=t.xx,yyy=t.yy;
		ma[xxx][yyy]='.';
		for(int l=(n-1)*2-1;l>=3;l-=2){
			bool f1=t.mask&(1<<l),f2=t.mask&(1<<l-1);
			if(!f1&&!f2){
				xxx+=dx[1];
				yyy+=dy[1];
			}
			else if(!f1&&f2){
				xxx+=dx[2];
				yyy+=dy[2];
			}
			else if(f1&&!f2){
				xxx+=dx[3];
				yyy+=dy[3];
			}
			else if(f1&&f2){
				xxx+=dx[4];
				yyy+=dy[4];
			}
			ma[xxx][yyy]='.';
		}
	}
	cout<<-1;
	return;
}
int main(){
	/*freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);*/
	cin>>a>>b;
	rep(i,a){
		rep(j,b){
			cin>>ma[i][j];
			if(ma[i][j]>='1'&&ma[i][j]<='9'){
				n=max(n,ma[i][j]-'0');
				x[ma[i][j]-'0']=i;
				y[ma[i][j]-'0']=j;
			}
		}
	}
	rep(i,n-1){
		rep(j,4){
			if(x[i]+dx[j]==x[i+1]&&y[i]+dy[j]==y[i+1]){
				if(j==2){
					mas|=(1<<(n-i)*2-2);
				}
				else if(j==3){
					mas|=(1<<(n-i)*2-1);
				}
				else if(j==4){
					mas|=(1<<((n-i)*2)-1)|(1<<(n-i)*2-2);
				}
				break;
			}
		}
	}
	bfs();
	return 0;
}


#pragma GCC optimize(2)
#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define rep(i,n) for(int i=1;i<=(n);i++)
#define rep0(i,n) for(int i=0;i<(n);i++)

const ll INF=1e9+7;
const int dx[5]={0,-1,0,1,0};
const int dy[5]={0,0,1,0,-1};
int n,a,b,mas;
char ma[16][16];
int x[10];
int y[10];
bool vis[16][16][1<<16];
struct hhh{
	int xx;
	int yy;
	int mask;
	int step;
};
void bfs(){
	queue<hhh> q;
	hhh w;
	w.xx=x[1];
	w.yy=y[1];
	w.mask=mas;
	w.step=0;
	q.push(w);
	while(!q.empty()){
		hhh t=q.front();
		q.pop();
		if(ma[t.xx][t.yy]=='@'){
			cout<<t.step;
			return;
		}
		vis[t.xx][t.yy][t.mask]=1;
		/*int xxx=t.xx,yyy=t.yy;
		ma[xxx][yyy]='#';
		for(int l=(n-1)*2-1;l>=3;l-=2){
			bool f1=t.mask&(1<<l),f2=t.mask&(1<<l-1);
			if(!f1&&!f2){
				xxx+=dx[1];
				yyy+=dy[1];
			}
			else if(!f1&&f2){
				xxx+=dx[2];
				yyy+=dy[2];
			}
			else if(f1&&!f2){
				xxx+=dx[3];
				yyy+=dy[3];
			}
			else if(f1&&f2){
				xxx+=dx[4];
				yyy+=dy[4];
			}
			ma[xxx][yyy]='#';
		}*/
		rep(i,4){
			int dxx=t.xx+dx[i];
			int dyy=t.yy+dy[i];
			if(dxx>=1&&dxx<=a&&dyy>=1&&dyy<=b&&ma[dxx][dyy]!='#'){
				int k=t.mask;
				k=k>>2;
				if(i==4){
					k|=(1<<(n-1)*2-2);
				}
				else if(i==1){
					k|=(1<<(n-1)*2-1);
				}
				else if(i==2){
					k|=(1<<(n-1)*2-1)|(1<<(n-1)*2-2);
				}
				if(vis[dxx][dyy][k]){
					continue;
				}
				bool flag=1;
				int xxx=t.xx,yyy=t.yy;
				ma[xxx][yyy]='#';
				for(int l=(n-1)*2-1;l>=3;l-=2){
					bool f1=t.mask&(1<<l),f2=t.mask&(1<<l-1);
					if(!f1&&!f2){
						xxx+=dx[1];
						yyy+=dy[1];
						if(xxx==dxx&&yyy==dyy){
							flag=0;
							break;
						}
					}
					else if(!f1&&f2){
						xxx+=dx[2];
						yyy+=dy[2];
						if(xxx==dxx&&yyy==dyy){
							flag=0;
							break;
						}
					}
					else if(f1&&!f2){
						xxx+=dx[3];
						yyy+=dy[3];
						if(xxx==dxx&&yyy==dyy){
							flag=0;
							break;
						}
					}
					else if(f1&&f2){
						xxx+=dx[4];
						yyy+=dy[4];
						if(xxx==dxx&&yyy==dyy){
							flag=0;
							break;
						}
					}
				}
				if(!flag){
					continue;
				}
				hhh e;
				e.xx=dxx;
				e.yy=dyy;
				e.mask=k;
				e.step=t.step+1;
				q.push(e);
			}
		}
		/*xxx=t.xx,yyy=t.yy;
		ma[xxx][yyy]='.';
		for(int l=(n-1)*2-1;l>=3;l-=2){
			bool f1=t.mask&(1<<l),f2=t.mask&(1<<l-1);
			if(!f1&&!f2){
				xxx+=dx[1];
				yyy+=dy[1];
			}
			else if(!f1&&f2){
				xxx+=dx[2];
				yyy+=dy[2];
			}
			else if(f1&&!f2){
				xxx+=dx[3];
				yyy+=dy[3];
			}
			else if(f1&&f2){
				xxx+=dx[4];
				yyy+=dy[4];
			}
			ma[xxx][yyy]='.';
		}*/
	}
	cout<<-1;
	return;
}
int main(){
	/*freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);*/
	cin>>a>>b;
	rep(i,a){
		rep(j,b){
			cin>>ma[i][j];
			if(ma[i][j]>='1'&&ma[i][j]<='9'){
				n=max(n,ma[i][j]-'0');
				x[ma[i][j]-'0']=i;
				y[ma[i][j]-'0']=j;
			}
		}
	}
	rep(i,n-1){
		rep(j,4){
			if(x[i]+dx[j]==x[i+1]&&y[i]+dy[j]==y[i+1]){
				if(j==2){
					mas|=(1<<(n-i)*2-2);
				}
				else if(j==3){
					mas|=(1<<(n-i)*2-1);
				}
				else if(j==4){
					mas|=(1<<((n-i)*2)-1)|(1<<(n-i)*2-2);
				}
				break;
			}
		}
	}
	bfs();
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define rep(i,n) for(int i=1;i<=(n);i++)
#define rep0(i,n) for(int i=0;i<(n);i++)

const ll INF=1e9+7;
const int dx[5]={0,-1,0,1,0};
const int dy[5]={0,0,1,0,-1};
int n,a,b,mas;
char ma[16][16];
int x[10];
int y[10];
bool vis[16][16][1<<16];
struct hhh{
	int xx;
	int yy;
	int mask;
	int step;
};
void bfs(){
	queue<hhh> q;
	hhh w;
	w.xx=x[1];
	w.yy=y[1];
	w.mask=mas;
	w.step=0;
	q.push(w);
	while(!q.empty()){
		hhh t=q.front();
		q.pop();
		if(ma[t.xx][t.yy]=='@'){
			cout<<t.step;
			return;
		}
		vis[t.xx][t.yy][t.mask]=1;
		/*int xxx=t.xx,yyy=t.yy;
		ma[xxx][yyy]='#';
		for(int l=(n-1)*2-1;l>=3;l-=2){
			bool f1=t.mask&(1<<l),f2=t.mask&(1<<l-1);
			if(!f1&&!f2){
				xxx+=dx[1];
				yyy+=dy[1];
			}
			else if(!f1&&f2){
				xxx+=dx[2];
				yyy+=dy[2];
			}
			else if(f1&&!f2){
				xxx+=dx[3];
				yyy+=dy[3];
			}
			else if(f1&&f2){
				xxx+=dx[4];
				yyy+=dy[4];
			}
			ma[xxx][yyy]='#';
		}*/
		rep(i,4){
			int dxx=t.xx+dx[i];
			int dyy=t.yy+dy[i];
			if(dxx>=1&&dxx<=a&&dyy>=1&&dyy<=b&&ma[dxx][dyy]!='#'){
				int k=t.mask;
				k=k>>2;
				if(i==4){
					k|=(1<<(n-1)*2-2);
				}
				else if(i==1){
					k|=(1<<(n-1)*2-1);
				}
				else if(i==2){
					k|=(1<<(n-1)*2-1)|(1<<(n-1)*2-2);
				}
				if(vis[dxx][dyy][k]){
					continue;
				}
				bool flag=1;
				int xxx=t.xx,yyy=t.yy;
				ma[xxx][yyy]='#';
				for(int l=(n-1)*2-1;l>=3;l-=2){
					bool f1=t.mask&(1<<l),f2=t.mask&(1<<l-1);
					if(!f1&&!f2){
						xxx+=dx[1];
						yyy+=dy[1];
						if(xxx==dxx&&yyy==dyy){
							flag=0;
							break;
						}
					}
					else if(!f1&&f2){
						xxx+=dx[2];
						yyy+=dy[2];
						if(xxx==dxx&&yyy==dyy){
							flag=0;
							break;
						}
					}
					else if(f1&&!f2){
						xxx+=dx[3];
						yyy+=dy[3];
						if(xxx==dxx&&yyy==dyy){
							flag=0;
							break;
						}
					}
					else if(f1&&f2){
						xxx+=dx[4];
						yyy+=dy[4];
						if(xxx==dxx&&yyy==dyy){
							flag=0;
							break;
						}
					}
				}
				if(!flag){
					continue;
				}
				hhh e;
				e.xx=dxx;
				e.yy=dyy;
				e.mask=k;
				e.step=t.step+1;
				q.push(e);
			}
		}
		/*xxx=t.xx,yyy=t.yy;
		ma[xxx][yyy]='.';
		for(int l=(n-1)*2-1;l>=3;l-=2){
			bool f1=t.mask&(1<<l),f2=t.mask&(1<<l-1);
			if(!f1&&!f2){
				xxx+=dx[1];
				yyy+=dy[1];
			}
			else if(!f1&&f2){
				xxx+=dx[2];
				yyy+=dy[2];
			}
			else if(f1&&!f2){
				xxx+=dx[3];
				yyy+=dy[3];
			}
			else if(f1&&f2){
				xxx+=dx[4];
				yyy+=dy[4];
			}
			ma[xxx][yyy]='.';
		}*/
	}
	cout<<-1;
	return;
}
int main(){
	/*freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);*/
	cin>>a>>b;
	rep(i,a){
		rep(j,b){
			cin>>ma[i][j];
			if(ma[i][j]>='1'&&ma[i][j]<='9'){
				n=max(n,ma[i][j]-'0');
				x[ma[i][j]-'0']=i;
				y[ma[i][j]-'0']=j;
			}
		}
	}
	rep(i,n-1){
		rep(j,4){
			if(x[i]+dx[j]==x[i+1]&&y[i]+dy[j]==y[i+1]){
				if(j==2){
					mas|=(1<<(n-i)*2-2);
				}
				else if(j==3){
					mas|=(1<<(n-i)*2-1);
				}
				else if(j==4){
					mas|=(1<<((n-i)*2)-1)|(1<<(n-i)*2-2);
				}
				break;
			}
		}
	}
	bfs();
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define rep(i,n) for(int i=1;i<=(n);i++)
#define rep0(i,n) for(int i=0;i<(n);i++)

const ll INF=1e9+7;
const int dx[5]={0,-1,0,1,0};
const int dy[5]={0,0,1,0,-1};
int n,a,b,mas;
char ma[16][16];
int x[10];
int y[10];
bool vis[16][16][1<<16];
struct hhh{
	int xx;
	int yy;
	int mask;
	int step;
};
void bfs(){
	queue<hhh> q;
	hhh w;
	w.xx=x[1];
	w.yy=y[1];
	w.mask=mas;
	w.step=0;
	q.push(w);
	while(!q.empty()){
		hhh t=q.front();
		q.pop();
		if(ma[t.xx][t.yy]=='@'){
			cout<<t.step;
			return;
		}
		vis[t.xx][t.yy][t.mask]=1;
		rep(i,4){
			int dxx=t.xx+dx[i];
			int dyy=t.yy+dy[i];
			if(dxx>=1&&dxx<=a&&dyy>=1&&dyy<=b&&ma[dxx][dyy]!='#'){
				bool flag=1;
				int xxx=t.xx,yyy=t.yy;
				for(int l=(n-1)*2-1;l>=3;l-=2){
					bool f1=t.mask&(1<<l),f2=t.mask&(1<<l-1);
					if(!f1&&!f2){
						xxx+=dx[1];
						yyy+=dy[1];
						if(xxx==dxx&&yyy==dyy){
							flag=0;
							break;
						}
					}
					else if(!f1&&f2){
						xxx+=dx[2];
						yyy+=dy[2];
						if(xxx==dxx&&yyy==dyy){
							flag=0;
							break;
						}
					}
					else if(f1&&!f2){
						xxx+=dx[3];
						yyy+=dy[3];
						if(xxx==dxx&&yyy==dyy){
							flag=0;
							break;
						}
					}
					else if(f1&&f2){
						xxx+=dx[4];
						yyy+=dy[4];
						if(xxx==dxx&&yyy==dyy){
							flag=0;
							break;
						}
					}
				}
				if(!flag){
					continue;
				}int k=t.mask;
				k=k>>2;
				if(i==4&&n>1){
					k|=(1<<(n-1)*2-2);
				}
				else if(i==1&&n>1){
					k|=(1<<(n-1)*2-1);
				}
				else if(i==2&&n>1){
					k|=(1<<(n-1)*2-1)|(1<<(n-1)*2-2);
				}
				if(vis[dxx][dyy][k]){
					continue;
				}
				hhh e;
				e.xx=dxx;
				e.yy=dyy;
				e.mask=k;
				e.step=t.step+1;
				q.push(e);
			}
		}
	}
	cout<<-1;
	return;
}
int main(){
	/*freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);*/
	cin>>a>>b;
	rep(i,a){
		rep(j,b){
			cin>>ma[i][j];
			if(ma[i][j]>='1'&&ma[i][j]<='9'){
				n=max(n,ma[i][j]-'0');
				x[ma[i][j]-'0']=i;
				y[ma[i][j]-'0']=j;
			}
		}
	}
	rep(i,n-1){
		rep(j,4){
			if(x[i]+dx[j]==x[i+1]&&y[i]+dy[j]==y[i+1]){
				if(j==2){
					mas|=(1<<(n-i)*2-2);
				}
				else if(j==3){
					mas|=(1<<(n-i)*2-1);
				}
				else if(j==4){
					mas|=(1<<((n-i)*2)-1)|(1<<(n-i)*2-2);
				}
				break;
			}
		}
	}
	bfs();
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define rep(i,n) for(int i=1;i<=(n);i++)
#define rep0(i,n) for(int i=0;i<(n);i++)

const ll INF=1e9+7;
const int dx[5]={-1,0,1,0};
const int dy[5]={0,1,0,-1};
int n,a,b,mas;
char ma[16][16];
int x[10];
int y[10];
bool vis[16][16][1<<16];
struct hhh{
	int xx;
	int yy;
	int mask;
	int step;
};
bool loser(int x,int y,hhh h){
	int xxx=h.xx;
	int yyy=h.yy;
	int k=h.mask;
	rep(i,n-2){
		if(x==xxx&&y==yyy){
			return 0;
		}
		xxx-=dx[k&3];
		yyy-=dy[k&3];
		k>>=2;
	}
	if(x==xxx&&y==yyy){
		return 0;
	}
	return 1;
}
void bfs(){
	queue<hhh> q;
	hhh w;
	w.xx=x[1];
	w.yy=y[1];
	w.mask=mas;
	w.step=0;
	q.push(w);
	while(!q.empty()){
		hhh t=q.front();
//		cout<<t.xx<<' '<<t.yy<<' '<<t.mask<<' '<<t.step<<endl;
		q.pop();
		if(ma[t.xx][t.yy]=='@'){
			cout<<t.step;
			return;
		}
		vis[t.xx][t.yy][t.mask]=1;
		rep0(i,4){
			int dxx=t.xx+dx[i];
			int dyy=t.yy+dy[i];
			if(dxx>=1&&dxx<=a&&dyy>=1&&dyy<=b&&ma[dxx][dyy]!='#'){
				if(!loser(dxx,dyy,t)){
					continue;
				}
				int k=t.mask;
				k<<=2;
				k&=((1<<2*(n-1))-1);
				k|=i;
				if(vis[dxx][dyy][k]){
					continue;
				}
				hhh e;
				e.xx=dxx;
				e.yy=dyy;
				e.mask=k;
				e.step=t.step+1;
				q.push(e);
			}
		}
	}
	cout<<-1;
	return;
}
int main(){
	/*freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);*/
	cin>>a>>b;
	rep(i,a){
		rep(j,b){
			cin>>ma[i][j];
			if(ma[i][j]>='1'&&ma[i][j]<='9'){
				n=max(n,ma[i][j]-'0');
				x[ma[i][j]-'0']=i;
				y[ma[i][j]-'0']=j;
			}
		}
	}
	for(int i=n;i>=1;i--){
		rep0(j,4){
			if(x[i+1]+dx[j]==x[i]&&y[i+1]+dy[j]==y[i]){
				mas<<=2;
				mas|=j;
				break;
			}
		}
	}
	bfs();
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define rep(i,n) for(int i=1;i<=(n);i++)
#define rep0(i,n) for(int i=0;i<(n);i++)

const ll INF=1e9+7;
const int dx[5]={-1,0,1,0};
const int dy[5]={0,1,0,-1};
int n,a,b,mas;
char ma[16][16];
int x[10];
int y[10];
bool vis[16][16][1<<16];
struct hhh{
	int xx;
	int yy;
	int mask;
	int step;
};
bool loser(int dir,int x,int y,hhh h){
	int xxx=h.xx;
	int yyy=h.yy;
	int k=h.mask;
	rep(i,n-2){
		if(x==xxx&&y==yyy){
			return 0;
		}
		xxx-=dx[k&3];
		yyy-=dy[k&3];
		k>>=2;
	}
	if(x==xxx&&y==yyy){
		return 0;
	}
	if(x==xxx-dx[k&3]&&y==yyy-dy[k&3]&&((k&3)==dir+2||(k&3)+2==dir)){
		return 0;
	}
	return 1;
}
void bfs(){
	queue<hhh> q;
	hhh w;
	w.xx=x[1];
	w.yy=y[1];
	w.mask=mas;
	w.step=0;
	q.push(w);
	vis[w.xx][w.yy][w.mask]=1;
	while(!q.empty()){
		hhh t=q.front();
//		cout<<t.xx<<' '<<t.yy<<' '<<t.mask<<' '<<t.step<<endl;
		q.pop();
		if(ma[t.xx][t.yy]=='@'){
			cout<<t.step;
			return;
		}
		rep0(i,4){
			int dxx=t.xx+dx[i];
			int dyy=t.yy+dy[i];
			if(dxx>=1&&dxx<=a&&dyy>=1&&dyy<=b&&ma[dxx][dyy]!='#'&&loser(i,dxx,dyy,t)){
				hhh e;
				e.mask=t.mask&(1<<(2*(n-1))-1)|i;
				if(vis[dxx][dyy][e.mask]){
					continue;
				}
				vis[dxx][dyy][e.mask]=1;
				e.xx=dxx;
				e.yy=dyy;
				e.step=t.step+1;
				q.push(e);
			}
		}
	}
	cout<<-1;
	return;
}
int main(){
	/*freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);*/
	cin>>a>>b;
	rep(i,a){
		rep(j,b){
			cin>>ma[i][j];
			if(ma[i][j]>='1'&&ma[i][j]<='9'){
				n=max(n,ma[i][j]-'0');
				x[ma[i][j]-'0']=i;
				y[ma[i][j]-'0']=j;
			}
		}
	}
	for(int i=n;i>=1;i--){
		rep0(j,4){
			if(x[i+1]+dx[j]==x[i]&&y[i+1]+dy[j]==y[i]){
				mas<<=2;
				mas|=j;
				break;
			}
		}
	}
	bfs();
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define rep(i,n) for(int i=1;i<=(n);i++)
#define rep0(i,n) for(int i=0;i<(n);i++)

const ll INF=1e9+7;
const int dx[5]={-1,0,1,0};
const int dy[5]={0,1,0,-1};
int n,a,b,mas;
char ma[16][16];
int x[10];
int y[10];
bool vis[16][16][1<<16];
struct hhh{
	int xx;
	int yy;
	int mask;
	int step;
};
bool loser(int dir,int x,int y,hhh h){
	int xxx=h.xx;
	int yyy=h.yy;
	int k=h.mask;
	rep(i,n-2){
		if(x==xxx&&y==yyy){
			return 0;
		}
		xxx-=dx[k&3];
		yyy-=dy[k&3];
		k>>=2;
	}
	if(x==xxx&&y==yyy){
		return 0;
	}
	if((k&3)==dir+2||(k&3)+2==dir){
		return 0;
	}
	return 1;
}
void bfs(){
	queue<hhh> q;
	hhh w;
	w.xx=x[1];
	w.yy=y[1];
	w.mask=mas;
	w.step=0;
	q.push(w);
	while(!q.empty()){
		hhh t=q.front();
//		cout<<t.xx<<' '<<t.yy<<' '<<t.mask<<' '<<t.step<<endl;
		q.pop();
		if(ma[t.xx][t.yy]=='@'){
			cout<<t.step;
			return;
		}
		vis[t.xx][t.yy][t.mask]=1;
		rep0(i,4){
			int dxx=t.xx+dx[i];
			int dyy=t.yy+dy[i];
			if(dxx>=1&&dxx<=a&&dyy>=1&&dyy<=b&&ma[dxx][dyy]!='#'){
				int k=t.mask;
				k<<=2;
				k&=((1<<2*(n-1))-1);
				k|=i;
				if(vis[dxx][dyy][k]){
					continue;
				}
				if(!loser(i,dxx,dyy,t)){
					continue;
				}
				hhh e;
				e.xx=dxx;
				e.yy=dyy;
				e.mask=k;
				e.step=t.step+1;
				q.push(e);
			}
		}
	}
	cout<<-1;
	return;
}
int main(){
	/*freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);*/
	cin>>a>>b;
	rep(i,a){
		rep(j,b){
			cin>>ma[i][j];
			if(ma[i][j]>='1'&&ma[i][j]<='9'){
				n=max(n,ma[i][j]-'0');
				x[ma[i][j]-'0']=i;
				y[ma[i][j]-'0']=j;
			}
		}
	}
	for(int i=n;i>=1;i--){
		rep0(j,4){
			if(x[i+1]+dx[j]==x[i]&&y[i+1]+dy[j]==y[i]){
				mas<<=2;
				mas|=j;
				break;
			}
		}
	}
	bfs();
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define rep(i,n) for(int i=1;i<=(n);i++)
#define rep0(i,n) for(int i=0;i<(n);i++)

const ll INF=1e9+7;
const int dx[5]={0,-1,0,1,0};
const int dy[5]={0,0,1,0,-1};
int n,a,b,mas;
char ma[16][16];
int x[10];
int y[10];
bool vis[16][16][1<<16];
struct hhh{
	int xx;
	int yy;
	int mask;
	int step;
};
void bfs(){
	queue<hhh> q;
	hhh w;
	w.xx=x[1];
	w.yy=y[1];
	w.mask=mas;
	w.step=0;
	q.push(w);
	while(!q.empty()){
		hhh t=q.front();
		cout<<t.xx<<' '<<t.yy<<' '<<t.mask<<' '<<t.step<<endl;
		q.pop();
		if(ma[t.xx][t.yy]=='@'){
			cout<<t.step;
			return;
		}
		vis[t.xx][t.yy][t.mask]=1;
		rep(i,4){
			int dxx=t.xx+dx[i];
			int dyy=t.yy+dy[i];
			if(dxx>=1&&dxx<=a&&dyy>=1&&dyy<=b&&ma[dxx][dyy]!='#'){
				bool flag=1;
				int xxx=t.xx,yyy=t.yy;
				for(int l=(n-1)*2-1;l>=3;l-=2){
					bool f1=t.mask&(1<<l),f2=t.mask&(1<<l-1);
					if(!f1&&!f2){
						xxx+=dx[1];
						yyy+=dy[1];
						if(xxx==dxx&&yyy==dyy){
							flag=0;
							break;
						}
					}
					else if(!f1&&f2){
						xxx+=dx[2];
						yyy+=dy[2];
						if(xxx==dxx&&yyy==dyy){
							flag=0;
							break;
						}
					}
					else if(f1&&!f2){
						xxx+=dx[3];
						yyy+=dy[3];
						if(xxx==dxx&&yyy==dyy){
							flag=0;
							break;
						}
					}
					else if(f1&&f2){
						xxx+=dx[4];
						yyy+=dy[4];
						if(xxx==dxx&&yyy==dyy){
							flag=0;
							break;
						}
					}
				}
				if(!flag){
					continue;
				}int k=t.mask;
				k=k>>2;
				if(i==4&&n>1){
					k|=(1<<(n-1)*2-2);
				}
				else if(i==1&&n>1){
					k|=(1<<(n-1)*2-1);
				}
				else if(i==2&&n>1){
					k|=(1<<(n-1)*2-1)|(1<<(n-1)*2-2);
				}
				if(vis[dxx][dyy][k]){
					continue;
				}

				hhh e;
				e.xx=dxx;
				e.yy=dyy;
				e.mask=k;
				e.step=t.step+1;
				q.push(e);
			}
		}
	}
	cout<<-1;
	return;
}
int main(){
	/*freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);*/
	cin>>a>>b;
	rep(i,a){
		rep(j,b){
			cin>>ma[i][j];
			if(ma[i][j]>='1'&&ma[i][j]<='9'){
				n=max(n,ma[i][j]-'0');
				x[ma[i][j]-'0']=i;
				y[ma[i][j]-'0']=j;
			}
		}
	}
	rep(i,n-1){
		rep(j,4){
			if(x[i]+dx[j]==x[i+1]&&y[i]+dy[j]==y[i+1]){
				if(j==2){
					mas|=(1<<(n-i)*2-2);
				}
				else if(j==3){
					mas|=(1<<(n-i)*2-1);
				}
				else if(j==4){
					mas|=(1<<((n-i)*2)-1)|(1<<(n-i)*2-2);
				}
				break;
			}
		}
	}
	bfs();
	return 0;
}


#pragma GCC optimize(2)
#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define rep(i,n) for(int i=1;i<=(n);i++)
#define rep0(i,n) for(int i=0;i<(n);i++)

const ll INF=1e9+7;
const int dx[5]={0,-1,0,1,0};
const int dy[5]={0,0,1,0,-1};
int n,a,b,mas;
char ma[16][16];
int x[10];
int y[10];
bool vis[16][16][1<<15];
struct hhh{
	int xx;
	int yy;
	int mask;
	int step;
};
void bfs(){
	queue<hhh> q;
	hhh w;
	w.xx=x[1];
	w.yy=y[1];
	w.mask=mas;
	w.step=0;
	q.push(w);
	while(!q.empty()){
		hhh t=q.front();
		q.pop();
		if(ma[t.xx][t.yy]=='@'){
			cout<<t.step;
			return;
		}
		vis[t.xx][t.yy][t.mask]=1;
		int xxx=t.xx,yyy=t.yy;
		ma[xxx][yyy]='#';
		for(int l=(n-1)*2-1;l>=3;l-=2){
			bool f1=t.mask&(1<<l),f2=t.mask&(1<<l-1);
			if(!f1&&!f2){
				xxx+=dx[1];
				yyy+=dy[1];
			}
			else if(!f1&&f2){
				xxx+=dx[2];
				yyy+=dy[2];
			}
			else if(f1&&!f2){
				xxx+=dx[3];
				yyy+=dy[3];
			}
			else if(f1&&f2){
				xxx+=dx[4];
				yyy+=dy[4];
			}
			ma[xxx][yyy]='#';
		}
		rep(i,4){
			int dxx=t.xx+dx[i];
			int dyy=t.yy+dy[i];
			if(dxx>=1&&dxx<=a&&dyy>=1&&dyy<=b&&ma[dxx][dyy]!='#'){
				int k=t.mask;
				k=k>>2;
				if(i==4){
					k|=(1<<(n-1)*2-2);
				}
				else if(i==1){
					k|=(1<<(n-1)*2-1);
				}
				else if(i==2){
					k|=(1<<(n-1)*2-1)|(1<<(n-1)*2-2);
				}
				if(vis[dxx][dyy][k]){
					continue;
				}
				bool flag=1;
				hhh e;
				e.xx=dxx;
				e.yy=dyy;
				e.mask=k;
				e.step=t.step+1;
				q.push(e);
			}
		}
		xxx=t.xx,yyy=t.yy;
		ma[xxx][yyy]='.';
		for(int l=(n-1)*2-1;l>=3;l-=2){
			bool f1=t.mask&(1<<l),f2=t.mask&(1<<l-1);
			if(!f1&&!f2){
				xxx+=dx[1];
				yyy+=dy[1];
			}
			else if(!f1&&f2){
				xxx+=dx[2];
				yyy+=dy[2];
			}
			else if(f1&&!f2){
				xxx+=dx[3];
				yyy+=dy[3];
			}
			else if(f1&&f2){
				xxx+=dx[4];
				yyy+=dy[4];
			}
			ma[xxx][yyy]='.';
		}
	}
	cout<<-1;
	return;
}
int main(){
	/*freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);*/
	cin>>a>>b;
	rep(i,a){
		rep(j,b){
			cin>>ma[i][j];
			if(ma[i][j]>='1'&&ma[i][j]<='9'){
				n=max(n,ma[i][j]-'0');
				x[ma[i][j]-'0']=i;
				y[ma[i][j]-'0']=j;
			}
		}
	}
	rep(i,n-1){
		rep(j,4){
			if(x[i]+dx[j]==x[i+1]&&y[i]+dy[j]==y[i+1]){
				if(j==2){
					mas|=(1<<(n-i)*2-2);
				}
				else if(j==3){
					mas|=(1<<(n-i)*2-1);
				}
				else if(j==4){
					mas|=(1<<((n-i)*2)-1)|(1<<(n-i)*2-2);
				}
				break;
			}
		}
	}
	bfs();
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define rep(i,n) for(int i=1;i<=(n);i++)
#define rep0(i,n) for(int i=0;i<(n);i++)

const ll INF=1e9+7;
const int dx[5]={0,-1,0,1,0};
const int dy[5]={0,0,1,0,-1};
int n,a,b,mas;
char ma[16][16];
int x[10];
int y[10];
bool vis[16][16]/*[1<<16]*/;
struct hhh{
	int xx;
	int yy;
	int mask;
	int step;
};
void bfs(){
	queue<hhh> q;
	hhh w;
	w.xx=x[1];
	w.yy=y[1];
	w.mask=mas;
	w.step=0;
	q.push(w);
	while(!q.empty()){
		hhh t=q.front();
		q.pop();
		if(ma[t.xx][t.yy]=='@'){
			cout<<t.step;
			return;
		}
		vis[t.xx][t.yy]/*[t.mask]*/=1;
		rep(i,4){
			int dxx=t.xx+dx[i];
			int dyy=t.yy+dy[i];
			if(dxx>=1&&dxx<=a&&dyy>=1&&dyy<=b&&ma[dxx][dyy]!='#'){
				int k=t.mask;
				k=k>>2;
				if(i==4&&n>1){
					k|=(1<<(n-1)*2-2);
				}
				else if(i==1&&n>1){
					k|=(1<<(n-1)*2-1);
				}
				else if(i==2&&n>1){
					k|=(1<<(n-1)*2-1)|(1<<(n-1)*2-2);
				}
				if(vis[dxx][dyy]/*[k]*/){
					continue;
				}
				bool flag=1;
				int xxx=t.xx,yyy=t.yy;
				for(int l=(n-1)*2-1;l>=3;l-=2){
					bool f1=t.mask&(1<<l),f2=t.mask&(1<<l-1);
					if(!f1&&!f2){
						xxx+=dx[1];
						yyy+=dy[1];
						if(xxx==dxx&&yyy==dyy){
							flag=0;
							break;
						}
					}
					else if(!f1&&f2){
						xxx+=dx[2];
						yyy+=dy[2];
						if(xxx==dxx&&yyy==dyy){
							flag=0;
							break;
						}
					}
					else if(f1&&!f2){
						xxx+=dx[3];
						yyy+=dy[3];
						if(xxx==dxx&&yyy==dyy){
							flag=0;
							break;
						}
					}
					else if(f1&&f2){
						xxx+=dx[4];
						yyy+=dy[4];
						if(xxx==dxx&&yyy==dyy){
							flag=0;
							break;
						}
					}
				}
				if(!flag){
					continue;
				}
				hhh e;
				e.xx=dxx;
				e.yy=dyy;
				e.mask=k;
				e.step=t.step+1;
				q.push(e);
			}
		}
	}
	cout<<-1;
	return;
}
int main(){
	/*freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);*/
	cin>>a>>b;
	rep(i,a){
		rep(j,b){
			cin>>ma[i][j];
			if(ma[i][j]>='1'&&ma[i][j]<='9'){
				n=max(n,ma[i][j]-'0');
				x[ma[i][j]-'0']=i;
				y[ma[i][j]-'0']=j;
			}
		}
	}
	rep(i,n-1){
		rep(j,4){
			if(x[i]+dx[j]==x[i+1]&&y[i]+dy[j]==y[i+1]){
				if(j==2){
					mas|=(1<<(n-i)*2-2);
				}
				else if(j==3){
					mas|=(1<<(n-i)*2-1);
				}
				else if(j==4){
					mas|=(1<<((n-i)*2)-1)|(1<<(n-i)*2-2);
				}
				break;
			}
		}
	}
	bfs();
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define rep(i,n) for(int i=1;i<=(n);i++)
#define rep0(i,n) for(int i=0;i<(n);i++)

const ll INF=1e9+7;
const int dx[5]={-1,0,1,0};
const int dy[5]={0,1,0,-1};
int n,a,b,mas;
char ma[16][16];
int x[10];
int y[10];
bool vis[16][16][1<<16];
struct hhh{
	int xx;
	int yy;
	int mask;
	int step;
};
bool loser(int x,int y,hhh h){
	int xxx=h.xx;
	int yyy=h.yy;
	int k=h.mask;
	rep(i,n-2){
		if(x==xxx&&y==yyy){
			return 0;
		}
		xxx-=dx[k&3];
		yyy-=dy[k&3];
		k>>=2;
	}
	if(x==xxx&&y==yyy){
		return 0;
	}
	return 1;
}
void bfs(){
	queue<hhh> q;
	hhh w;
	w.xx=x[1];
	w.yy=y[1];
	w.mask=mas;
	w.step=0;
	q.push(w);
	while(!q.empty()){
		hhh t=q.front();
//		cout<<t.xx<<' '<<t.yy<<' '<<t.mask<<' '<<t.step<<endl;
		q.pop();
		if(ma[t.xx][t.yy]=='@'){
			cout<<t.step;
			return;
		}
		vis[t.xx][t.yy][t.mask]=1;
		rep0(i,4){
			int dxx=t.xx+dx[i];
			int dyy=t.yy+dy[i];
			if(dxx>=1&&dxx<=a&&dyy>=1&&dyy<=b&&ma[dxx][dyy]!='#'){
				int k=t.mask;
				k<<=2;
				k&=((1<<2*(n-1))-1);
				k|=i;
				if(vis[dxx][dyy][k]){
					continue;
				}
				if(!loser(dxx,dyy,t)){
					continue;
				}
				hhh e;
				e.xx=dxx;
				e.yy=dyy;
				e.mask=k;
				e.step=t.step+1;
				q.push(e);
			}
		}
	}
	cout<<-1;
	return;
}
int main(){
	/*freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);*/
	cin>>a>>b;
	rep(i,a){
		rep(j,b){
			cin>>ma[i][j];
			if(ma[i][j]>='1'&&ma[i][j]<='9'){
				n=max(n,ma[i][j]-'0');
				x[ma[i][j]-'0']=i;
				y[ma[i][j]-'0']=j;
			}
		}
	}
	for(int i=n;i>=1;i--){
		rep0(j,4){
			if(x[i+1]+dx[j]==x[i]&&y[i+1]+dy[j]==y[i]){
				mas<<=2;
				mas|=j;
				break;
			}
		}
	}
	bfs();
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define rep(i,n) for(int i=1;i<=(n);i++)
#define rep0(i,n) for(int i=0;i<(n);i++)

const ll INF=1e9+7;
const int dx[5]={0,-1,0,1,0};
const int dy[5]={0,0,1,0,-1};
int n,a,b,mas;
char ma[16][16];
int x[10];
int y[10];
bool vis[16][16][1<<16];
struct hhh{
	int xx;
	int yy;
	int mask;
	int step;
};
void bfs(){
	queue<hhh> q;
	hhh w;
	w.xx=x[1];
	w.yy=y[1];
	w.mask=mas;
	w.step=0;
	q.push(w);
	while(!q.empty()){
		hhh t=q.front();
		q.pop();
		if(ma[t.xx][t.yy]=='@'){
			return;
		}
		vis[t.xx][t.yy][t.mask]=1;
		rep(i,4){
			int dxx=t.xx+dx[i];
			int dyy=t.yy+dy[i];
			if(dxx>=1&&dxx<=a&&dyy>=1&&dyy<=b&&ma[dxx][dyy]!='#'){
				bool flag=1;
				int xxx=t.xx,yyy=t.yy;
				for(int l=(n-1)*2-1;l>=3;l-=2){
					bool f1=t.mask&(1<<l),f2=t.mask&(1<<l-1);
					if(!f1&&!f2){
						xxx+=dx[1];
						yyy+=dy[1];
						if(xxx==dxx&&yyy==dyy){
							flag=0;
							break;
						}
					}
					else if(!f1&&f2){
						xxx+=dx[2];
						yyy+=dy[2];
						if(xxx==dxx&&yyy==dyy){
							flag=0;
							break;
						}
					}
					else if(f1&&!f2){
						xxx+=dx[3];
						yyy+=dy[3];
						if(xxx==dxx&&yyy==dyy){
							flag=0;
							break;
						}
					}
					else if(f1&&f2){
						xxx+=dx[4];
						yyy+=dy[4];
						if(xxx==dxx&&yyy==dyy){
							flag=0;
							break;
						}
					}
				}
				if(!flag){
					continue;
				}int k=t.mask;
				k=k>>2;
				if(i==4&&n>1){
					k|=(1<<(n-1)*2-2);
				}
				else if(i==1&&n>1){
					k|=(1<<(n-1)*2-1);
				}
				else if(i==2&&n>1){
					k|=(1<<(n-1)*2-1)|(1<<(n-1)*2-2);
				}
				if(vis[dxx][dyy][k]){
					continue;
				}
				hhh e;
				e.xx=dxx;
				e.yy=dyy;
				e.mask=k;
				e.step=t.step+1;
				q.push(e);
			}
		}
	}
	cout<<-1;
	return;
}
int main(){
	/*freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);*/
	cin>>a>>b;
	rep(i,a){
		rep(j,b){
			cin>>ma[i][j];
			if(ma[i][j]>='1'&&ma[i][j]<='9'){
				n=max(n,ma[i][j]-'0');
				x[ma[i][j]-'0']=i;
				y[ma[i][j]-'0']=j;
			}
		}
	}
	rep(i,n-1){
		rep(j,4){
			if(x[i]+dx[j]==x[i+1]&&y[i]+dy[j]==y[i+1]){
				if(j==2){
					mas|=(1<<(n-i)*2-2);
				}
				else if(j==3){
					mas|=(1<<(n-i)*2-1);
				}
				else if(j==4){
					mas|=(1<<((n-i)*2)-1)|(1<<(n-i)*2-2);
				}
				break;
			}
		}
	}
	bfs();
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define pb push_back
#define mp make_pair
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define calm (l+r)>>1
const int INF=2139062143;

const int maxn=20;
int n,m,maxlen;
ll base;
char pic[maxn][maxn];
bool vis[15][15][65536];
int go[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int rev[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
struct snake{
    int x,y;
    int status;
    snake():x(0),y(0),status(0){}
}start;
inline bool judge(int x,int y){
    return x>=0&&x<n&&y>=0&&y<m;
}
void startpos(){//计算出一开始蛇的状态，和一些其他数据
    maxlen=0;
    int x,y;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++)if(pic[i][j]!='#'&&pic[i][j]!='@'){
            if(pic[i][j]-'0'>maxlen){
                x=i;y=j;
                maxlen=pic[i][j]-'0';
            }
            if(pic[i][j]=='1'){
                start.x=i;start.y=j;
            }
        }
    }
    base=(1<<((maxlen-1)*2))-1;//蛇的最大长度所需要的全1二进制数，用来去掉多余位置的数据
    int &status=start.status;
    status=0;
    while(pic[x][y]!='1'){
        for(int i=0;i<4;i++){
            int xx=x+rev[i][0],yy=y+rev[i][1];
            if(judge(xx,yy)&&pic[xx][yy]==pic[x][y]-1){
                status<<=2;
                status|=i;
                x=xx;y=yy;
                break;
            }
        }
    }
}
#define F first
#define S second
bool bomb(int xx,int yy,snake s){//从蛇头开始移动，并判断是否发生碰撞
    int &status=s.status;
    int x=s.x,y=s.y;
    if(x==xx&&y==yy)return false;
    for(int i=1;i<maxlen-1;i++){
        x+=go[status&3][0];
        y+=go[status&3][1];
        status>>=2;
        if(x==xx&&y==yy)return false;
    }
    return true;
}
int BFS(){
    queue<pair<snake,int>> Q;
    Q.push(mp(start,0));
    vis[start.x][start.y][start.status]=true;
    while(!Q.empty()){
        snake st=Q.front().F;
        int step=Q.front().S;
        if(pic[st.x][st.y]=='@')return step;
        Q.pop();
        for(int i=0;i<4;i++){
            int xx=st.x+rev[i][0];
            int yy=st.y+rev[i][1];
            if(judge(xx,yy)&&pic[xx][yy]!='#'&&bomb(xx,yy,st)){
                snake next;
                next.x=xx;next.y=yy;
                next.status=((st.status<<2)&base)|i;
                if(!vis[xx][yy][next.status]){
                    vis[xx][yy][next.status]=true;
                    Q.push(mp(next,step+1));
                }
            }
        }
    }
    return -1;
}
int main(){
    //freopen("D://input.txt","r",stdin);
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++){
        scanf("%s",pic[i]);
    }
    startpos();
    printf("%d\n",BFS());
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define rep(i,n) for(int i=1;i<=(n);i++)
#define rep0(i,n) for(int i=0;i<(n);i++)

const ll INF=1e9+7;
const int dx[5]={-1,0,1,0};
const int dy[5]={0,1,0,-1};
int n,a,b,mas;
char ma[16][16];
int x[10];
int y[10];
bool vis[16][16][1<<16];
struct hhh{
	int xx;
	int yy;
	int mask;
	int step;
};
bool loser(int dir,int x,int y,hhh h){
	int xxx=h.xx;
	int yyy=h.yy;
	int k=h.mask;
	rep(i,n-2){
		if(x==xxx&&y==yyy){
			return 0;
		}
		xxx-=dx[k&3];
		yyy-=dy[k&3];
		k>>=2;
	}
	if(x==xxx&&y==yyy){
		return 0;
	}
	if(x==xxx-dx[k&3]&&y==yyy-dy[k&3]&&((k&3)==dir+2||(k&3)+2==dir)){
		return 0;
	}
	return 1;
}
void bfs(){
	queue<hhh> q;
	hhh w;
	w.xx=x[1];
	w.yy=y[1];
	w.mask=mas;
	w.step=0;
	q.push(w);
	vis[w.xx][w.yy][w.mask]=1;
	while(!q.empty()){
		hhh t=q.front();
//		cout<<t.xx<<' '<<t.yy<<' '<<t.mask<<' '<<t.step<<endl;
		q.pop();
		if(ma[t.xx][t.yy]=='@'){
			cout<<t.step;
			return;
		}
		rep0(i,4){
			int dxx=t.xx+dx[i];
			int dyy=t.yy+dy[i];
			if(dxx>=1&&dxx<=a&&dyy>=1&&dyy<=b&&ma[dxx][dyy]!='#'&&loser(i,dxx,dyy,t)){
				int k=t.mask;
				k<<=2;
				k&=((1<<2*(n-1))-1);
				k|=i;
				if(vis[dxx][dyy][k]){
					continue;
				}
				vis[dxx][dyy][k]=1;
				hhh e;
				e.xx=dxx;
				e.yy=dyy;
				e.mask=k;
				e.step=t.step+1;
				q.push(e);
			}
		}
	}
	cout<<-1;
	return;
}
int main(){
	/*freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);*/
	cin>>a>>b;
	rep(i,a){
		rep(j,b){
			cin>>ma[i][j];
			if(ma[i][j]>='1'&&ma[i][j]<='9'){
				n=max(n,ma[i][j]-'0');
				x[ma[i][j]-'0']=i;
				y[ma[i][j]-'0']=j;
			}
		}
	}
	for(int i=n;i>=1;i--){
		rep0(j,4){
			if(x[i+1]+dx[j]==x[i]&&y[i+1]+dy[j]==y[i]){
				mas<<=2;
				mas|=j;
				break;
			}
		}
	}
	bfs();
	return 0;
}


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

const int maxn=20;

struct Node{
	int x,y,sta;
	Node(){};
	Node(int x,int y,int sta):x(x),y(y),sta(sta){};
};

//0(00):up 1(01):down 2(10):le 3(11):ri
//(1111111111111111)65536
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
int rx[4]={1,-1,0,0};
int ry[4]={0,0,1,-1};

int n,m,k;//k�ߵĽ���
int step[15][15][65536];
char M[maxn][maxn];

bool isok(int sta){
	int x=0,y=0;
	for(int i=0;i<k-1;i++){
		int j=(sta>>(2*i))&3;
		x+=rx[j];y+=ry[j];
		if(x==0&&y==0)return false;
	}
	return true;
}

void BFS(int x,int y,int sta){
	memset(step,-1,sizeof(step));
	step[x][y][sta]=0;
	queue<Node> Q;
	Q.push(Node(x,y,sta));
	while(!Q.empty()){
		Node temp=Q.front();
		Q.pop();
		Node nxt;
		for(int i=0;i<4;i++){
			nxt.x=temp.x+dx[i];
			nxt.y=temp.y+dy[i];
			nxt.sta=((temp.sta<<2)&MX)|i;
			if(
				nxt.x>=0&&nxt.x<n&&
				nxt.y>=0&&nxt.y<m&&//��Խ��
				M[nxt.x][nxt.y]!='#'&&//��ײǽ
				isok(nxt.sta)&&//nxt״̬��ײ���Լ�
				step[nxt.x][nxt.y][nxt.sta]==-1
			){
				step[nxt.x][nxt.y][nxt.sta]=step[temp.x][temp.y][temp.sta]+1;
				if(
					M[nxt.x][nxt.y]=='@'
					//net����ͷ�Ե�����
				){
					cout<<step[nxt.x][nxt.y][nxt.sta]<<endl;
					return;
				}
				Q.push(nxt);
			}
		}
	}
	cout<<-1<<endl;
	return;
}

int calc_sta(int x,int y){
	int sta=0;
	for(int i=0;i<k-1;i++){
		for(int j=0;j<4;j++){
			int n_x=x+rx[j];
			int n_y=y+ry[j];
			if(M[n_x][n_y]==M[x][y]+1){
				sta|=(j)<<(2*i);
				x=n_x;
				y=n_y;
				break;
			}
		}
	}
	return sta;
}

int main(){
	cin>>n>>m;
	int x,y,sta;
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++){
			cin>>M[i][j];
			if(M[i][j]>='1'&&M[i][j]<='9')k=max(M[i][j]-'0',k);
			if(M[i][j]=='1')x=i,y=j;
		}
	sta=calc_sta(x,y);
	BFS(x,y,sta);
	return 0;
}


#include <bits/stdc++.h>
#define re register
#define ll long long
using namespace std;
const int maxn=16;
struct snk{
	int x,y,d;
	ll sta;
};
int n,m;
int sx,sy,ex,ey,lenth;
ll sst;
int dx[4]={0,1,-1,0};
int dy[4]={1,0,0,-1};
char a[maxn][maxn];
bool book[maxn][maxn][1<<18];
//bool used[maxn][maxn];
queue<snk> q;
bool judge(int x,int y){
	if(x<1 || x>n || y<1 || y>m || a[x][y]=='#') return false;
	return true;
}
void fi(int x,int y){
	if(a[x][y]-'0'==lenth) return;
	//used[x][y]=1;
	for(re int k=0;k<4;k++){
		int nx=x+dx[k],ny=y+dy[k];
		if(judge(nx,ny) && a[nx][ny]==a[x][y]+1){
			//cout<<nx<<" "<<ny<<" "<<dx[k]<<" "<<dy[k]<<endl;
			//cout<<k<<endl;
			sst+=(k)<<((a[x][y]-'0'-1)*2);
			fi(nx,ny);
		}
	}
}
int bfs(){
	snk ttmp;
	ttmp.x=sx;ttmp.y=sy;ttmp.sta=sst;ttmp.d=0;
	q.push(ttmp);
	book[sx][sy][sst]=1;
	while(!q.empty()){
		for(re int k=0;k<4;k++){
			snk tmp=q.front();
			int nx=tmp.x+dx[k],ny=tmp.y+dy[k];
			if(judge(nx,ny)){
				if(nx==ex && ny==ey) return tmp.d+1;
				snk c=tmp;
				tmp.x=nx;
				tmp.y=ny;
				tmp.sta&=(1<<(lenth*2-1))-1;
				tmp.sta<<=2;
				tmp.sta+=k^3;
				if(book[nx][ny][tmp.sta]) continue;
				int flag=1;
				for(re int t=1;t<lenth-1;t++){
					//cout<<c.x<<" "<<c.y<<endl;
					//cout<<(c.sta&3)<<endl;
					c.x+=dx[c.sta&3];
					c.y+=dy[c.sta&3];
					if(nx==c.x && ny==c.y){
						flag=0;
						break;
					}
					c.sta>>=2;
				}
				//cout<<endl;
				if(!flag) continue;
				book[nx][ny][tmp.sta]=1;
				tmp.d++;
				//cout<<nx<<" "<<ny<<endl;
				q.push(tmp);
			}
		}
		q.pop();
	}
	return -1;
}
void init(){
	cin>>n>>m;
	for(re int i=1;i<=n;i++){
		for(re int j=1;j<=m;j++){
			cin>>a[i][j];
			if(a[i][j]=='1'){
				sx=i;
				sy=j;
			}
			if(a[i][j]=='@'){
				ex=i;
				ey=j;
			}
			if(1<=a[i][j]-'0' && a[i][j]-'0'<=9){
				lenth=max(lenth,a[i][j]-'0');
			}
		}
	}
	//cout<<lenth<<endl;
	//cout<<ex<<" "<<ey<<endl;
	fi(sx,sy);
}
int main(){
	init();
	cout<<bfs()<<endl;
	return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define pb push_back
#define mp make_pair
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define calm (l+r)>>1
const int INF=2139062143;

const int maxn=20;
int n,m,maxlen;
ll base;
char pic[maxn][maxn];
bool vis[15][15][65536];
int go[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int rev[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
struct snake{
    int x,y;
    int status;
    snake():x(0),y(0),status(0){}
}start;
inline bool judge(int x,int y){
    return x>=0&&x<n&&y>=0&&y<m;
}
void startpos(){
    maxlen=0;
    int x,y;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++)if(pic[i][j]!='#'&&pic[i][j]!='@'){
            if(pic[i][j]-'0'>maxlen){
                x=i;y=j;
                maxlen=pic[i][j]-'0';
            }
            if(pic[i][j]=='1'){
                start.x=i;start.y=j;
            }
        }
    }
    base=(1<<((maxlen-1)*2))-1;
    int &status=start.status;
    status=0;
    while(pic[x][y]!='1'){
        for(int i=0;i<4;i++){
            int xx=x+rev[i][0],yy=y+rev[i][1];
            if(judge(xx,yy)&&pic[xx][yy]==pic[x][y]-1){
                status<<=2;
                status|=i;
                x=xx;y=yy;
                break;
            }
        }
    }
}
#define F first
#define S second
bool bomb(int xx,int yy,snake s){
    int &status=s.status;
    int x=s.x,y=s.y;
    if(x==xx&&y==yy)return false;
    for(int i=1;i<maxlen-1;i++){
        x+=go[status&3][0];
        y+=go[status&3][1];
        status>>=2;
        if(x==xx&&y==yy)return false;
    }
    return true;
}
int BFS(){
    queue<pair<snake,int>> Q;
    Q.push(mp(start,0));
    vis[start.x][start.y][start.status]=true;
    while(!Q.empty()){
        snake st=Q.front().F;
        int step=Q.front().S;
        if(pic[st.x][st.y]=='@')return step;
        Q.pop();
        for(int i=0;i<4;i++){
            int xx=st.x+rev[i][0];
            int yy=st.y+rev[i][1];
            if(judge(xx,yy)&&pic[xx][yy]!='#'&&bomb(xx,yy,st)){
                snake next;
                next.x=xx;next.y=yy;
                next.status=((st.status<<2)&base)|i;
                if(!vis[xx][yy][next.status]){
                    vis[xx][yy][next.status]=true;
                    Q.push(mp(next,step+1));
                }
            }
        }
    }
    return -1;
}
int main(){
    //freopen("D://input.txt","r",stdin);
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++){
        scanf("%s",pic[i]);
    }
    startpos();
    printf("%d\n",BFS());
    return 0;
}


#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
using namespace std;
#define x first
#define y second
struct node
{
    vector<pair<int,int> > v;
    int step;
};
int n,m;
queue<node> q;
int ex,ey;
bool used[20][20];
int dx[5]={0,0,0,1,-1};
int dy[5]={0,1,-1,0,0};
char a[20][20];
map<vector<pair<int,int> >,bool> f;
bool inside(int x,int y)
{
    if(1<=x&&x<=n&&1<=y&&y<=m)
        return 1;
    return 0;
}
bool check(int x,int y)
{
	if('0'<=a[x][y]&&a[x][y]<='9')
		return 1;
	for(int i=1;i<=4;i++)
	{
		int tx=x+dx[i];
		int ty=y+dy[i];
		if(used[tx][ty]==0&&inside(tx,ty)&&a[tx][ty]!='#')
		{
			used[tx][ty]=1;
			if(check(tx,ty))
				return 1;
		}
	}
	return 0;
}
bool pd(vector<pair<int,int> > v,pair<int,int> t)
{
    for(int i=1;i<v.size()-1;i++)
        if(t==v[i])
            return 0;
    return 1;
}
int main()
{
	ios::sync_with_stdio(false);
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            cin>>a[i][j];
            if(a[i][j]=='1')
            {
                vector<pair<int,int> > v;
                v.push_back(make_pair(i,j));
                q.push((node){v,0});
            }
            if(a[i][j]=='@')
            {
                ex=i;
                ey=j;
            }
        }
    if(!check(ex,ey))
    {
    	cout<<"-1";
    	return 0;
	}
    for(int k=2;k<=9;k++)
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                if(a[i][j]=='0'+k)
                    q.front().v.push_back(make_pair(i,j));
	while(!q.empty())
    {
        node t=q.front();
        q.pop();
        if(t.v[0].x==ex&&t.v[0].y==ey)
        {
            cout<<t.step;
            return 0;
        }
        for(int i=1;i<=4;i++)
        {
            int tx=t.v[0].x+dx[i];
            int ty=t.v[0].y+dy[i];
            if(inside(tx,ty)&&pd(t.v,make_pair(tx,ty))&&a[tx][ty]!='#')
            {
            	node temp=t;
                for(int i=temp.v.size()-1;i>=1;i--)
                    temp.v[i]=temp.v[i-1];
                temp.v[0]=make_pair(tx,ty);
                temp.step ++;
                if(f[temp.v]==0)
                {
                	q.push(temp);
                	f[temp.v]=1;
				}

            }
        }
    }
    cout<<"-1";
    return 0;
}


#include <cstdio>
#include <queue>
#include <map>
#define reg register
using namespace std;

inline int csq(int x){
    if(x < 0)
        return -x;
    return x;
}

char a[20][20];
int len, n, m, dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
struct node{
    int x[10], y[10], s;
}beg;
queue<node> q;
map<long long, bool> mapo;

inline long long hx(node x){
    long long tot = 123456;
    for(reg int i = 2; i <= len; i++){
        tot = (tot + (x.x[i] * m + x.y[i]) * 1ll * (x.x[i - 1] * m + x.y[i - 1]) % 998353017) * 1ll * 5201314 % 998353017;
    }
    return tot;
}

int main(){
    scanf("%d%d", &n, &m);
    for(reg int i = 1; i <= n; i++){
        scanf("\n");
        for(reg int j = 1; j <= m; j++){
            scanf("%c", &a[i][j]);
            if(a[i][j] >= '1' && a[i][j] <= '8'){
                beg.x[a[i][j] - '0'] = i;
                beg.y[a[i][j] - '0'] = j;
                beg.s = 0;
                if(a[i][j] - '0' > len)
                    len = a[i][j] - '0';
            }
        }
    }
    mapo[hx(beg)] = 1;
    q.push(beg);
    while(!q.empty()){
        node ne, t = q.front();
        q.pop();
        for(reg int i = 0; i < 4; i++){
            ne = t;
            ne.x[1] = t.x[1] + dir[i][0];
            ne.y[1] = t.y[1] + dir[i][1];
            if(ne.x[1] < 1 || ne.x[1] > n || ne.y[1] < 1 || ne.y[1] > m || a[ne.x[1]][ne.y[1]] == '#')
                continue;
            bool flag = 0;
            for(reg int j = 2; j <= len - 1; j ++){
                if(ne.x[1] == t.x[j] && ne.y[1] == t.y[j]){
                    flag = 1;
                }
            }
            if(flag)
                continue;
            if(a[ne.x[1]][ne.y[1]] == '@'){
                printf("%d\n", t.s + 1);
                return 0;
            }
            for(reg int i = 2; i <= len; i++){
                ne.x[i] = t.x[i - 1];
                ne.y[i] = t.y[i - 1];
            }
            ne.s = t.s + 1;
            long long tmp = hx(ne);
            if(!mapo[tmp]){
                mapo[tmp] = 1;
                q.push(ne);
            }
        }
    }
    printf("-1\n");
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m,t=0,x,y;
int dx[5]={0,1,0,-1,0},dy[5]={0,0,1,0,-1};
bool a[20][20]={};
pair<int,int> s[10];
queue<vector<pair<int,int> > > q;
map<vector<pair<int,int> >,int> dis;
map<vector<pair<int,int> >,bool> vis;
void bfs(vector<pair<int,int> > k)
{
    q.push(k);
    dis[k]=0;
    vis[k]=true;
    while(!q.empty())
    {
        vector<pair<int,int> > vx=q.front();
        bool now[20][20];
        memcpy(now,a,sizeof(now));
        for(int i=0;i<(int)vx.size();i++) now[vx[i].first][vx[i].second]=true;
        for(int i=1;i<=4;i++)
        {
            int tx=vx[0].first+dx[i],ty=vx[0].second+dy[i];
            if(!now[tx][ty])
            {
                vector<pair<int,int> > vy;
                vy.push_back(make_pair(tx,ty));
                for(int i=0;i<(int)vx.size()-1;i++) vy.push_back(vx[i]);
                if(!vis[vy])
                {
                    q.push(vy);
                    dis[vy]=dis[vx]+1;
                    vis[vy]=true;
                    if(tx==x&&ty==y)
                    {
                        cout<<dis[vy];
                        exit(0);
                    }
                }
            }
        }
        q.pop();
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>m;
    for(int i=1;i<=n;i++) a[i][0]=a[i][m+1]=true;
    for(int j=1;j<=m;j++) a[0][j]=a[n+1][j]=true;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            char ch;
            cin>>ch;
            if(ch>='0'&&ch<='9')
            {
                t=max(t,ch-'0');
                s[ch-'0']=make_pair(i,j);
            }
            else if(ch=='#') a[i][j]=true;
            else if(ch=='@') x=i,y=j;
        }
    vector<pair<int,int> > v;
    for(int i=1;i<t;i++) v.push_back(s[i]);
    bfs(v);
    cout<<"-1";
	return 0;
}


#include <map>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define Jzm 1
#define diaoda ==
#define Lqy 1
#define LL long long
#define MAXN 20
#define Pa pair<LL, LL>
#define Int register LL
#define INF 0x3f3f3f3f
using namespace std;
inline LL Abs(LL x)
{
	if (x > 0)
		return x;
	return - x;
}
inline LL Min(LL x,LL y)
{
	return x < y ? x : y;
}
inline LL Max(LL x,LL y)
{
	return x > y ? x : y;
}
inline void read(LL &x)
{
	x = 0;
	LL f = 1;
	char s = getchar();
	while (s < '0' || s > '9')
	{
		if (s == '-')
			f = -1;
		s = getchar();
	}
	while (s >= '0' && s <= '9')
	{
		x = (x << 3) + (x << 1) + (s ^ 48);
		s = getchar();
	}
	x *= f;
}
LL Dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
char Maze[MAXN][MAXN];
struct node
{
	deque<Pa > Snake; LL Step;
	node(){}
	node(deque<Pa > SNAKE,LL STEP)
	{
		Snake = SNAKE;
		Step = STEP;
	}
}Now;
deque<Pa > Begin;
map<deque<Pa >, bool> Map;
bool Vis[MAXN][MAXN];
LL n, m;
bool Check(LL x,LL y)
{
	if (Maze[x][y] >= '1' && Maze[x][y] <= '9')
		return 1;
	for (Int i = 0; i < 4; ++ i)
	{
		LL tox = x + Dir[i][0];
		LL toy = y + Dir[i][1];
		if (tox < 1 || tox > n || toy < 1 || toy > m || Maze[tox][toy] == '#' || Vis[tox][toy])
			continue;
		Vis[tox][toy] = 1;
		if ( Check(tox, toy) )
			return 1;
	}
	return 0;
}
int main()
{
	read( n ); read( m );
	for (Int i = 1; i <= n; ++ i)
		scanf("%s", Maze[i] + 1);
	for (Int i = 1; i <= n; ++ i)
		for (Int j = 1; j <= m; ++ j)
			if (Maze[i][j] == '@')
				if (! Check(i, j))
					return ! printf("-1");
	LL Len = 1;
	do
	{
		for (Int i = 1; i <= n; ++ i)
			for (Int j = 1; j <= m; ++ j)
			{
				if (Maze[i][j] - '0' == Len)
				{
					Len ++;
					Begin.push_back(Pa(i, j));
					goto JZM;
				}
			}
		break;
		JZM:;
	} while (Jzm diaoda Lqy);
	Len --;
	queue<node> Q;
	Q.push(node(Begin, 0));
	LL Ans = -1;
	while (! Q.empty())
	{
		Now = Q.front();
		Q.pop();
		if (Maze[Now.Snake[0].first][Now.Snake[0].second] == '@')
		{
			Ans = Now.Step;
			break;
		}
		Pa Head = Now.Snake.front();
		LL Hx = Head.first, Hy = Head.second;
		for (Int i = 0; i < 4; ++ i)
		{
			LL tox = Hx + Dir[i][0];
			LL toy = Hy + Dir[i][1];
			if (tox < 1 || tox > n || toy < 1 || toy > m || Maze[tox][toy] == '#')
				continue;
			deque<Pa > Temp = Now.Snake;
			Temp.pop_front();
			bool Flag = 0;
			for (Int i = 2; i < Len; ++ i)
			{
				Pa Jd = Temp.front(); Temp.pop_front();
				if (Jd.first == tox && Jd.second == toy)
				{
					Flag = 1;
					break;
				}
			}
			if ( Flag )
				continue;
			Temp = Now.Snake;
			Temp.pop_back();
			Temp.push_front(Pa(tox, toy));
			if (! Map[Temp])
			{
				Q.push(node(Temp, Now.Step + 1));
				Map[Temp] = 1;
			}
		}
	}
	printf("%lld", Ans);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
using namespace std;
#define x first
#define y second
struct node
{
    vector<pair<int,int> > v;
    int step;
};
int n,m;
queue<node> q;
int ex,ey;
bool used[25][25];
int dx[5]={0,0,0,1,-1};
int dy[5]={0,1,-1,0,0};
char a[20][20];
map<vector<pair<int,int> >,bool> f;
bool inside(int x,int y)
{
    if(1<=x&&x<=n&&1<=y&&y<=m)
        return 1;
    return 0;
}
bool check(int x,int y)
{
	if('0'<=a[x][y]&&a[x][y]<='9')
		return 1;
	for(int i=1;i<=4;i++)
	{
		int tx=x+dx[i];
		int ty=y+dy[i];
		if(used[tx][ty]==0&&inside(tx,ty)&&a[tx][ty]!='#')
		{
			used[tx][ty]=1;
			if(check(tx,ty))
				return 1;
		}
	}
	return 0;
}
bool pd(vector<pair<int,int> > v,pair<int,int> t)
{
    for(int i=1;i<v.size()-1;i++)
        if(t==v[i])
            return 0;
    return 1;
}
int main()
{
	ios::sync_with_stdio(false);
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        {
            cin>>a[i][j];
            if(a[i][j]=='1')
            {
            	a[i][j]='.';
                vector<pair<int,int> > v;
                v.push_back(make_pair(i,j));
                q.push((node){v,0});
            }
            if(a[i][j]=='@')
            {
                ex=i;
                ey=j;
            }
        }
    if(!check(ex,ey))
    {
    	cout<<"-1";
    	return 0;
	}
    for(int k=2;k<=9;k++)
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                if(a[i][j]=='0'+k)
                {
                    q.front().v.push_back(make_pair(i,j));
    				a[i][j]='.';
    			}
	while(!q.empty())
    {
        node t=q.front();
        q.pop();
        if(t.v[0].x==ex&&t.v[0].y==ey)
        {
            cout<<t.step;
            return 0;
        }
        for(int i=1;i<=4;i++)
        {
            int tx=t.v[0].x+dx[i];
            int ty=t.v[0].y+dy[i];
            if(inside(tx,ty)&&pd(t.v,make_pair(tx,ty))&&a[tx][ty]!='#')
            {
            	node temp=t;
                for(int i=temp.v.size()-1;i>=1;i--)
                    temp.v[i]=temp.v[i-1];
                temp.v[0]=make_pair(tx,ty);
                temp.step ++;
                if(f[temp.v]==0)
                {
                	q.push(temp);
                	f[temp.v]=1;
				}

            }
        }
    }
    cout<<"-1";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m,len,times,dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
char c[16][16];
pair<int,int>a[10];
string tot;
int vis[16][16];
queue<pair<pair<int,string>,deque<pair<int,int> > > >q;
bool ok(int x,int y,string z){
	if(x<0||x>=n||y<0||y>=m)return 0;
	if(vis[x][y]>50||c[x][y]=='#')return 0;
	if(z[x*m+y]=='0'+len)return 1;
	if(isdigit(z[x*m+y]))return 0;
	return 1;
}
deque<pair<int,int> >x;
int main(){
	cin>>n>>m;
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
		{
			cin>>c[i][j];
			tot+=c[i][j];
			if(isdigit(c[i][j]))
			{
				a[c[i][j]-'0']=make_pair(i,j);
				len=max(len,c[i][j]-'0');
			}
			if(c[i][j]=='1')
				vis[i][j]=1;
			if(c[i][j]=='@')
				a[0]=make_pair(i,j);
		}
	for(int i=1;i<=len;i++)
		x.push_back(a[i]);
	q.push(make_pair(make_pair(0,tot),x));
	while(!q.empty()){
		/*for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
				cout<<q.front().first.second[i*m+j];
			cout<<endl;
		}
		cout<<endl;
		*/deque<pair<int,int> >xxx;
		xxx=q.front().second;
		if(xxx[0]==a[0])
		{
			cout<<q.front().first.first;
			return 0;
		}
		for(int i=0;i<4;i++)
		{
			if(ok(xxx[0].first+dx[i],xxx[0].second+dy[i],q.front().first.second))
			{
				string tot2=q.front().first.second;
				tot2[(xxx[0].first+dx[i])*m+xxx[0].second+dy[i]]='1';
				for(int j=0;j<xxx.size()-1;j++)
					tot2[xxx[j].first*m+xxx[j].second]=char(j+'2');
				tot2[xxx[xxx.size()-1].first*m+xxx[xxx.size()-1].second]='.';
				vis[(xxx[0].first+dx[i])][xxx[0].second+dy[i]]++;
				xxx.push_front(make_pair(xxx[0].first+dx[i],xxx[0].second+dy[i]));
				pair<int,int>tmp=xxx[xxx.size()-1];
				xxx.pop_back();
				q.push(make_pair(make_pair(q.front().first.first+1,tot2),xxx));
				xxx.pop_front();
				xxx.push_back(tmp);
			}
		}
		q.pop();
	}
	cout<<-1;
}


#include<bits/stdc++.h>
using namespace std;
const int N=20,dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
struct snake
{
	int x[N],y[N],d;
};
int n,m,l,ex,ey,md;
char a[N][N];
set<int>s;
queue<snake>q;
int Hash(snake k)
{
	int h=0;
	for(int i=1;i<=l;i++)
		h=(h*17+k.x[i])*17+k.y[i];
	return h;
}
snake change(snake k,int dx,int dy)
{
	snake ans;
	int vis[N][N]={};
	for(int i=1;i<l;i++)
		vis[k.x[i]][k.y[i]]=1;
	ans.x[1]=k.x[1]+dx;
	ans.y[1]=k.y[1]+dy;
	if(ans.x[1]<1||ans.x[1]>n||ans.y[1]<1||ans.y[1]>m)
	{
		ans.x[1]=-1;
		return ans;
	}
	if(vis[ans.x[1]][ans.y[1]]||a[ans.x[1]][ans.y[1]]=='#')
	{
		ans.x[1]=-1;
		return ans;
	}
	for(int i=2;i<=l;i++)
	{
		ans.x[i]=k.x[i-1];
		ans.y[i]=k.y[i-1];
	}
	return ans;
}
int main()
{
	scanf("%d%d",&n,&m);
	snake b;
	b.d=0;
	for(int i=1;i<=n;i++)
	{
		scanf("%s",a[i]+1);
		for(int j=1;j<=m;j++)
		{
			if(a[i][j]>='1'&&a[i][j]<='9')
			{
				l=max(l,a[i][j]-'0');
				b.x[a[i][j]-'0']=i;
				b.y[a[i][j]-'0']=j;
				a[i][j]='.';
			}
			if(a[i][j]=='@')
			{
				ex=i;
				ey=j;
			}
		}
	}
	s.insert(Hash(b));
	q.push(b);
	while(!q.empty())
	{
		snake k=q.front();
		q.pop();
		for(int i=0;i<4;i++)
		{
			snake u=change(k,dx[i],dy[i]);
			if(u.x[1]==-1)
				continue;
			if(u.x[1]==ex&&u.y[1]==ey)
			{
				printf("%d\n",k.d+1);
				return 0;
			}
			if(s.count(Hash(u)))
				continue;
			u.d=k.d+1;
			s.insert(Hash(u));
			q.push(u);
		}
	}
	puts("-1");
	return 0;
}


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

char a[20][20];
int premask,vis[16][16][65536];
int n,m,k;
int go[4][2]={{-1,0},{0,1},{1,0},{0,-1}};

struct snake{
	int x,y,mask;
	snake(){};
	snake(int X,int Y,int Mask){
		x=X,y=Y,mask=Mask;
	}
};

int find(int x,int y,char cur){
	if(a[x-1][y]==cur+1) return 0;
	if(a[x][y+1]==cur+1) return 1;
	if(a[x+1][y]==cur+1) return 2;
	if(a[x][y-1]==cur+1) return 3;
}

bool judge(int tx,int ty,snake cur){
	int x=cur.x,y=cur.y,mask=cur.mask;
	for(int i=1;i<=k-2;i++,mask>>=2){
		x+=go[mask%4][0];
		y+=go[mask%4][1];
		if(x==tx && y==ty) return true;
	}
	return false;
}

int main(){
	//freopen("snake.in","r",stdin);
	scanf("%d %d\n",&n,&m);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			scanf("%c",&a[i][j]);
			if(a[i][j]>='1' && a[i][j]<='9')
				k=max(k,a[i][j]-'0');
		}
		scanf("\n");
	}
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++){
			if(a[i][j]>='1' && a[i][j]<k+'0')
				premask|=(find(i,j,a[i][j])<<(2*(a[i][j]-'1')));
		}
	queue<pair<snake,int> >Q;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			if(a[i][j]=='1'){
				vis[i][j][premask]=1;
				Q.push(pair<snake,int>{snake(i,j,premask),0});
			}
	int ans=-1;
	while(!Q.empty()){
		pair<snake,int>cur=Q.front();Q.pop();
		if(a[cur.first.x][cur.first.y]=='@'){ans=cur.second;break;}
		for(int i=0;i<=3;i++){
			int tx=cur.first.x+go[i][0];
			int ty=cur.first.y+go[i][1];
			int newmask=((cur.first.mask&((1<<(2*k-4))-1))<<2)|(i^2);
			if(a[tx][ty]=='#' || tx<1 || tx>n || ty<1 || ty>m || vis[tx][ty][newmask]) continue;
			if(judge(tx,ty,cur.first)) continue;
			vis[tx][ty][newmask]=1;
			Q.push(pair<snake,int>{snake(tx,ty,newmask),cur.second+1});
		}
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int dir[4][2]={1,0,0,1,-1,0,0,-1};
int n,m;
char g[20][20];
struct node
{
	int x[10],y[10];
	int num;
}s;
int pp,ppp;
int len;
queue<node> q;
int sx,sy;
bool vis[20000000];
int zhao(int a,int b,node s)
{
	for(int i=0;i<4;i++)
	{
         int x=s.x[b]+dir[i][0];
		 int y=s.y[b]+dir[i][1];
		 if(x==s.x[a]&&y==s.y[a]) return i;
	}
}
int get(int sx,int sy,node s)
{
	int h=sx*m+sy;
	for(int i=1;i<len;i++)
	h=h*4+zhao(i,i-1,s);
	return h;
}
bool pig()
{
	while(!q.empty())
	{
		node f=q.front();q.pop();
		int x=f.x[0],y=f.y[0];
		for(int i=0;i<4;i++)
		{
            int tx=x+dir[i][0];
			int ty=y+dir[i][1];
			node tm;
			tm.x[0]=tx;
			tm.y[0]=ty;
			if(tx<1||ty<1||tx>n||ty>m||g[tx][ty]=='#')
			continue;
			bool flag=false;
			for(int j=1;j<len-1;j++)
			if(tx==f.x[j]&&ty==f.y[j])
			{
			    flag=true;
				break;
			}
			if(flag) continue;
			for(int j=1;j<len;j++)
			{
				tm.x[j]=f.x[j-1];
				tm.y[j]=f.y[j-1];
			}
			int h=get(tx,ty,tm);
			if(!vis[h])
			{
				vis[h]=true;
				tm.num=f.num+1;
				q.push(tm);
			}
			if(tx==pp&&ty==ppp)
			return printf("%d",tm.num),true;
		}
	}
	return printf("-1"),true;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
	scanf("%s",g[i]+1);
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			if(g[i][j]=='@')
			{
				pp=i;
				ppp=j;
			}
			if(g[i][j]>='1'&&g[i][j]<='9')
			{
				if(g[i][j]=='1') sx=i,sy=j;
                s.x[g[i][j]-'1']=i;
				s.y[g[i][j]-'1']=j;
				len++;
				g[i][j]='.';
			}
		}
	}
    int h=get(sx,sy,s);
	vis[h]=true;
	s.num=0;
	q.push(s);
	pig();
	return 0;
}


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

int n,m,tot,book[20][20],I,J,Min=1000,ans;

char a[20][20];

int qr()
{
	int ret=0,f=1;
	char ch=getchar();
	while(!isdigit(ch))
	{
		f=ch=='-'?-1:1;
		ch=getchar();
	}
	while(isdigit(ch))
	{
		ret=ret*10+ch-'0';
		ch=getchar();
	}
	return ret*f;
}

void dfs(int x,int y,int r,int cnt)
{
	if(cnt>Min)
	{
		return ;
	}
	if(a[x][y]=='@')
	{
		if(cnt<Min)
		{
			Min=cnt;
		}
		return ;
	}
	if(x<1 || x>n || y<1 || y>m)
	{
		return ;
	}
	if(book[x][y]<r+1-tot)
	{
		book[x][y]=r;
		dfs(x,y+1,r+1,cnt+1);
		dfs(x+1,y,r+1,cnt+1);
		dfs(x,y-1,r+1,cnt+1);
		dfs(x-1,y,r+1,cnt+1);
		book[x][y]=0;
	}
	return ;
}

int main()
{
	n=qr(),m=qr();
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			cin>>a[i][j];
			if(isdigit(a[i][j]))
			{
				tot++;
				book[i][j]=a[i][j]-'0';
			}
			else if(a[i][j]=='#')
			{
				book[i][j]=2147483647;
			}
			if(a[i][j]=='1')
			{
				I=i;
				J=j;
			}
		}
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			if(book[i][j]>0 && book[i][j]<=9)
			{
				book[i][j]=tot+1-book[i][j];
			}
		}
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			cout<<book[i][j];
		}
		cout<<endl;
	}
	dfs(I,J+1,tot+1,1);
	dfs(I+1,J,tot+1,1);
	dfs(I,J-1,tot+1,1);
	dfs(I-1,J,tot+1,1);
	ans=Min==1000?-1:Min;
	cout<<ans;
	return 0;
}


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

struct point
{
	int x,y;
};
struct node
{
	point snake[11];
	int k;
};
char tat[16][16];
bool b[16][16];
int maxx=0;
queue<node>Q;
int var[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
int main()
{
	//freopen("1.out","w",stdout);
	memset(b,0,sizeof(b));
	long long n,m;
	cin>>n>>m;
	node tmp;
	point food;
	char c;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
		{
			cin>>c;
			if(c>='1'&&c<='9')
			{
				tat[i][j]='.';
				int v=c-'0';
				tmp.snake[v].x=i;
				tmp.snake[v].y=j;
				if(v>maxx)maxx=v;
			}
			else if(c=='@')
			{
				food.x=i;
				food.y=j;
				tat[i][j]=c;
			}
			else
			{
				tat[i][j]=c;
			}
		}
	tmp.k=0;
//	cout<<food.x<<" "<<food.y<<endl;
//	cout<<maxx<<endl;
//	for(int i=1;i<=n;i++)
//	{
//		for(int j=1;j<=m;j++)cout<<tat[i][j];
//		cout<<endl;
//	}
	Q.push(tmp);
	while(!Q.empty())
	{
		node cur=Q.front();Q.pop();
		//for(int i=1;i<=maxx;i++)cout<<cur.snake[i].x<<" "<<cur.snake[i].y<<endl;
		//cout<<"SB"<<cur.k<<endl;
		int x=cur.snake[1].x,y=cur.snake[1].y;
		if(x==food.x&&y==food.y)
		{
			cout<<cur.k;
			return 0;
		}
		if(cur.k==200)break;
		node tmp=cur;
	 	//cur.map[cur.snake[maxx].x][cur.snake[maxx].y]=tmp.map[cur.snake[maxx].x][cur.snake[maxx].y]='.';
		for(int i=maxx;i>=1;i--)
		{
			if(i<maxx)tat[cur.snake[i].x][cur.snake[i].y]='#';
			tmp.snake[i]=tmp.snake[i-1];
		}
		tmp.k++;
		for(int i=0;i<=3;i++)
		{
			if
			(
				(x+var[i][0]>0&&x+var[i][0]<=n&&y+var[i][1]>0&&y+var[i][1]<=m)
			&&
				(tat[x+var[i][0]][y+var[i][1]]=='.'||tat[x+var[i][0]][y+var[i][1]]=='@')
			&&
				(!b[x+var[i][0]][y+var[i][1]])
			)
			{
				b[x+var[i][0]][y+var[i][1]]=1;
				tmp.snake[1].x=x+var[i][0];tmp.snake[1].y=y+var[i][1];
				//tmp.map[x+var[i][0]][y+var[i][1]]='#';
				Q.push(tmp);
				//tmp.map[x+var[i][0]][y+var[i][1]]='.';
			}
		}
		for(int i=1;i<maxx;i++)tat[cur.snake[i].x][cur.snake[i].y]='.';
	}
	cout<<-1;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n,m,k,maxi;
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
int step[15][15][1<<16];
char used[20][20];
struct node
{
	int x,y;
	int state;
	node(){};
	node(int x,int y,int state):x(x),y(y),state(state){};
};
bool isok(int state)
{
	int x=0,y=0;
	for(int i=0;i<k-1;i++)
	{
		int vect=((x>>(2*i))&3);
		x-=dx[vect];
		y-=dy[vect];
		if(!x && !y) return false;
	}
	return true;
}
void bfs(int x,int y,int state)
{
	queue<node> Q;
	Q.push(node(x,y,state));
	memset(step,-1,sizeof(step));
	step[x][y][state]=0;
	while(!Q.empty())
	{
		node temp=Q.front();
		Q.pop();
		node nxt;
		for(int i=0;i<4;i++)
		{
			nxt.x=temp.x+dx[i];
			nxt.y=temp.y+dy[i];
			nxt.state=(temp.state<<2 && maxi)|i;
			//求新的state
			if(
				nxt.x>=0 && nxt.x<n &&
				nxt.y>=0 && nxt.y<m && //不越界
				used[nxt.x][nxt.y]!='#' && //没撞墙
				isok(nxt.state) && //没撞自己
				step[nxt.x][nxt.y][nxt.state]==-1//没访问过
			){
				step[nxt.x][nxt.y][nxt.state]=step[temp.x][temp.y][temp.state]+1;
				if(used[nxt.x][nxt.y]=='@')
				{
					cout<<step[nxt.x][nxt.y][nxt.state];
					return;
				}
				Q.push(nxt);
			}
		}
	}
	cout<<-1;
	return;
}
int calc_state(int x,int y)
{
	int ans=0;
	for(int i=0;i<k-1;i++)
	{
		for(int j=0;j<4;j++)
		{
			int nx=x-dx[i];
			int ny=y-dy[i];
			if(used[nx][ny]==used[x][y]+1)
			{
				ans|=j;
				x=nx;
				y=ny;
				break;
			}
		}
	}
	return ans;
}
int main()
{
	cin>>n>>m;
	int x,y,state;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			cin>>used[i][j];
			if(used[i][j]>='1' && used[i][j]<='9') k=max(k,used[i][j]-'0');
			if(used[i][j]=='1') x=i,y=j;
		}
	}
	maxi=1<<(2*(k-1))-1;
	state=calc_state(x,y);
	bfs(x,y,state);
	return 0;
}


#include<bits/stdc++.h>
#define vi vector<int>
#define pb push_back
using namespace std;
const int N=20;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
int n,m,d;
queue<vi>q;
map<vi,int>mp;
bool visit[N][N];
char c[N][N];
inline bool ok(int x2,int y2){return (x2>=0&&y2>=0&&x2<n&&y2<m);}
void dfs(int x,int y)
{
	if(c[x][y]=='#'||!ok(x,y)||visit[x][y])return;
	visit[x][y]=true;
	for(int i=0;i<4;i++)
	{
		int x2=dx[i]+x;
		int y2=dy[i]+y;
		dfs(x2,y2);
	}
}
int main()
{
	cin>>n>>m;
	vi v;
	for(int i=0;i<n;i++)cin>>c[i];
	for(char i='1';i<='9';i++)
	{
		bool found=false;
		for(int j=0;j<n;j++)
		{
			for(int l=0;l<m;l++)
			{
				if(c[j][l]==i)
				{
					v.pb(j);
					v.pb(l);
					found=true;
				}
			}
		}
		if(!found)
			break;
	}
	dfs(v[0],v[1]);
	bool can=false;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if(c[i][j]=='@'&&visit[i][j])
			{
				can=true;
				break;
			}
		}
		if(can)
			break;
	}
	if(!can)
	{
		cout<<-1;
		return 0;
	}
	mp[v]=0;
	q.push(v);
	/****problem in BFS****/
	while(!q.empty())
	{
		v=q.front();
		q.pop();
		d=mp[v];
		for(int i=0;i<4;i++)
		{
			int x2=v[0]+dx[i];
			int y2=v[1]+dy[i];
			if(!ok(x2,y2)||c[x2][y2]=='#')continue;
			bool eat=false;
			for(int j=0;j<v.size()/2-1;j++)
				if(x2==v[j*2]&&y2==v[j*2+1])
					eat=true;
			if(eat)continue;
			if(c[x2][y2]=='@')
			{
				cout<<d+1<<endl;
				return 0;
			}
			vi e(v.size());
			e[0]=x2;
			e[1]=y2;
			for(int j=0;j<v.size()-4;j++)
				e[2+j]=v[j];
			if(mp.find(e)==mp.end())
			{
				mp[e]=d+1;
				q.push(e);
			}
		}
	}
	cout<<-1;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,m,len=1;
int xx[4]={1,0,-1,0},yy[4]={0,-1,0,1};
char mp[17][17];
struct snake
{
	int head_x,head_y,body;
	bool operator< (const snake& P)const
	{
		if(head_x==P.head_x)
		{
			if(head_y==P.head_y)
				return body<P.body;
			return head_y<P.head_y;
		}
		return head_x<P.head_x;
	}
	bool legal()
	{
		if(head_x<1 || head_x>n || head_y<1 || head_y>m)return 0;
		if(mp[head_x][head_y]=='#')return 0;
		int nowx=head_x,nowy=head_y;
		for(int i=len-1;i>=1;i--)
		{
			nowx+=xx[(body>>2*i-2)&3];
			nowy+=yy[(body>>2*i-2)&3];
			if(nowx==head_x && nowy==head_y)return 0;
		}
		return 1;
	}
};
map<snake,int> used;
queue<snake> q;
int main()
{
	snake start;
	int tox=0,toy=0;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%s",mp[i]+1);
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
		{
			if(mp[i][j]=='1')
			{
				start.head_x=i;
				start.head_y=j;
				int nowx=i,nowy=j;
again:			for(int k=0;k<4;k++)
					if(mp[nowx+xx[k]][nowy+yy[k]]==mp[nowx][nowy]+1)
					{
						start.body=(start.body<<2)|k;
						nowx+=xx[k];
						nowy+=yy[k];
						len++;
						goto again;
					}
			}
			else if(mp[i][j]=='@')
			{
				tox=i;
				toy=j;
			}
		}
	q.push(start);
	used[start]=0;
	while(!q.empty())
	{
		snake now=q.front();q.pop();
		int ans=used[now];
//		printf("%d %d %d\n",now.head_x,now.head_y,now.body);
		if(now.head_x==tox && now.head_y==toy)
		{
			printf("%d\n",ans);
			return 0;
		}
		for(int i=0;i<4;i++)
		{

			snake to=now;
			to.head_x+=xx[i];
			to.head_y+=yy[i];
			to.body>>=2;
			to.body|=(i+2)%4<<len*2-4;
			if(used[to] || !to.legal())continue;
			q.push(to);
			used[to]=ans+1;
		}
	}
	puts("-1");
	return 0;
}


#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>

using namespace std;

#define REP(i,n) for((i)=0;(i)<(int)(n);(i)++)
#define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)

int X,Y,D;
string board[20];

int dx[] = {1,-1,0,0}, dy[] = {0,0,1,-1};
queue <vector <int> > q;
map <vector <int>, int> mp;

bool used[20][20];

void dfs(int x, int y){
   int i;

   if(x < 0 || x >= X || y < 0 || y >= Y || board[x][y] == '#' || used[x][y]) return;
   used[x][y] = true;

   REP(i,4) dfs(x+dx[i], y+dy[i]);
}

int main(void){
   int i,j,k;

   cin >> X >> Y;
   REP(i,X) cin >> board[i];

   vector <int> init;
   for(k=0;;k++){
       bool found = false;
       REP(i,X) REP(j,Y) if(board[i][j] == '1' + k){	//初始状态与蛇身长度
           found = true;
           init.push_back(i);
           init.push_back(j);
       }
       if(!found) break;	//超过最长就break
   }
   D = k;
   // a0 a1 a2 a3 a4 a5 a5
   // x0 y0 x1 y1

   dfs(init[0], init[1]);	//先dfs一次看看能否到达有果的格
   bool can = false;
   REP(i,X) REP(j,Y) if(board[i][j] == '@' && used[i][j]) can = true;
   if(!can){
       cout << -1 << endl;
       return 0;
   }

   q.push(init);
   mp[init] = 0;

   while(!q.empty()){
       vector <int> v = q.front();
       q.pop();
       int d = mp[v];	//步数

       REP(i,4){
           int x = v[0] + dx[i], y = v[1] + dy[i];
           if(x < 0 || x >= X || y < 0 || y >= Y || board[x][y] == '#') continue;
           bool bad = false;
           REP(j,D-1) if(x == v[2*j] && y == v[2*j+1]) bad = true;
           //身体
           if(bad) continue;

           if(board[x][y] == '@'){
               cout << d + 1 << endl;
               return 0;
           }

           vector <int> w(2*D);
           w[0] = x; w[1] = y;			//新的头
           REP(j,2*D-2) w[2+j] = v[j];	// 移动操作
           if(mp.find(w) == mp.end()){	//	没出现过入队
               mp[w] = d + 1;
               q.push(w);
           }
       }
   }

   cout << -1 << endl;		//队列空了还没吃到，就输出-1
   return 0;
}