#include<bits/stdc++.h>
using namespace std ;
#define F first
#define S second
const int Q = 1e4+5 ;
const int N = 67000005 ;
int dp[10] ;
vector<pair<pair<int,int>,pair<int,int>>> edges = {
{{0,0},{0,1}} , {{0,2},{1,2}} , {{2,1},{2,2}} , {{1,0},{2,0}} ,
{{0,1},{0,2}} , {{1,2},{2,2}} , {{2,0},{2,1}} , {{0,0},{1,0}} ,
{{0,1},{1,1}} , {{1,1},{1,2}} , {{1,1},{2,1}} , {{1,0},{1,1}} ,
{{0,0},{1,1}} , {{0,2},{1,1}} , {{1,1},{2,0}} , {{1,1},{2,2}} ,
{{0,1},{1,0}} , {{0,1},{1,2}} , {{1,2},{2,1}} , {{1,0},{2,1}} ,
{{0,0},{1,2}} , {{0,1},{2,2}} , {{1,2},{2,0}} , {{2,1},{0,0}} ,
{{0,2},{1,0}} , {{0,1},{2,2}} , {{1,2},{2,0}} , {{0,0},{2,1}}
} ;
vector<pair<pair<int,int>,pair<int,int>>> v ;
vector<pair<pair<int,int>,pair<int,int>>> BuildPattern(int n){
for(int i=1 ; i<7 ; i++) dp[i] = 3*(1<<(4*i)) + 4*dp[i-1] ;
vector<pair<pair<int,int>,pair<int,int>>> ans ;
for(int i=6 ; i>=0 ; i--){
if(i!=0 && n<4*dp[i-1]){
int temp = n/dp[i-1] ;
if(temp==1) ans.push_back(edges[4*i]) , ans.push_back(edges[4*i+2]) ; // 1010
else if(temp==2) ans.push_back(edges[4*i+1]) , ans.push_back(edges[4*i+3]) ; // 0101
else if(temp==3){ // 1111
ans.push_back(edges[4*i]) , ans.push_back(edges[4*i+1]) , ans.push_back(edges[4*i+2]) , ans.push_back(edges[4*i+3]) ;
}
n -= temp*dp[i-1] ;
}
else{
n -= 4*dp[i-1] ; //
int temp = n/(1<<(4*i)) ;
if(temp==0) ans.push_back(edges[4*i+3]) ; // 0001
else if(temp==1) ans.push_back(edges[4*i+2]) , ans.push_back(edges[4*i+3]) ; // 0011
else if(temp==2) ans.push_back(edges[4*i+1]) , ans.push_back(edges[4*i+2]) , ans.push_back(edges[4*i+3]) ; // 0111
n -= temp*(1<<(4*i)) ;
for(int j=0 ; j<i ; j++){
for(int l=0 ; l<=3 ; l++){
if((n>>l)&1) ans.push_back(edges[4*j+3-l]) ;
}
n/=16 ;
}
break ;
}
}
// for(auto it:ans) cout << it.F.F << ' ' << it.F.S << ' ' << it.S.F << ' ' << it.S.S << '\n' ; //
return ans ;
}
pair<int,int> f(int x , int y){
if(x==0 && y==0) return {2,0} ; if(x==0 && y==1) return {1,0} ; if(x==0 && y==2) return {0,0} ;
if(x==1 && y==0) return {2,1} ; if(x==1 && y==1) return {1,1} ; if(x==1 && y==2) return {0,1} ;
if(x==2 && y==0) return {2,2} ; if(x==2 && y==1) return {1,2} ; if(x==2 && y==2) return {0,2} ;
}
void rotate(){
for(int i=0 ; i<v.size() ; i++){
pair<int,int> a = v[i].F , b = v[i].S ;
v[i] = {f(a.F,a.S),f(b.F,b.S)} ;
}
}
int GetCardNumber(vector<pair<pair<int,int>,pair<int,int>>> v1){
for(int i=1 ; i<7 ; i++) dp[i] = 3*(1<<(4*i)) + 4*dp[i-1] ;
v = v1 ;
int cnt[10] ; memset(cnt,0,sizeof(cnt)) ;
for(auto it:v){
auto it1 = it ; swap(it1.F,it1.S) ;
for(int i=0 ; i<edges.size() ; i++){
if(edges[i]==it || edges[i]==it1) cnt[i/4]+=(1<<(3-i%4)) ;
}
}
// for(int i=0 ; i<7 ; i++) cout << cnt[i] << ' ' ; cout << '\n' ; //
int pos ;
for(int i=6 ; i>=0 ; i--){
if(cnt[i]==0 || cnt[i]==15 || cnt[i]==5 || cnt[i]==10) continue ;
pos = i ;
if(cnt[i]==1 || cnt[i]==3 || cnt[i]==7) break ;
if(cnt[i]==8 || cnt[i]==9 || cnt[i]==11) rotate() ;
else if(cnt[i]==4 || cnt[i]==12 || cnt[i]==13) rotate() , rotate() ;
else if(cnt[i]==2 || cnt[i]==6 || cnt[i]==14) rotate() , rotate() , rotate() ;
break ;
}
memset(cnt,0,sizeof(cnt)) ;
for(auto it:v){
auto it1 = it ; swap(it1.F,it1.S) ;
for(int i=0 ; i<edges.size() ; i++){
if(edges[i]==it || edges[i]==it1) cnt[i/4]+=(1<<(3-i%4)) ;
}
}
// cout << pos << '\n' ; for(int i=0 ; i<7 ; i++) cout << cnt[i] << ' ' ; cout << '\n' ; //
int ans = 0 ;
for(int i=0 ; i<pos ; i++){
ans+=cnt[i]*(1<<(4*i)) ;
}
ans += (__builtin_popcount(cnt[pos])-1)*(1<<(4*pos)) ;
for(int i=pos+1 ; i<7 ; i++){
if(cnt[i]==10) ans+=dp[i-1] ;
else if(cnt[i]==5) ans+=2*dp[i-1] ;
else if(cnt[i]==15) ans+=3*dp[i-1] ;
}
return ans ;
}
/*
int main(){
for(int i=1 ; i<=25 ; i++) cout << GetCardNumber(BuildPattern(i)) << ' ' ; cout << '\n' ;
return 0 ;
}
*/