#include #include #include using namespace std; char nxt(char c) { if(c=='Q') return 'U'; return c; } /* 5^n with some early breaking bad!!!! */ int main() { const int n = 16; vector a(n,vector(3,string())); for(int i : {0,1,1,1,1,2}) { string t; cin >> t; for(int j=0;j void { if(mycost>=ans) return; if(at==n) { ans=mycost; return; } for(int j=0;j<=2;++j) { char best = 'Z'+1; for(auto& nw : a[at][j]) { if(c<=nw) { best=min(best,nxt(nw)); } } if(best<='Z') { self(self,at+1,best,mycost+j); } } }; rec(rec,0,'A',0); if(ans==1e9) cout << "impossible\n"; else cout << ans << '\n'; }