//EGOI 2022 Chika Wants to Cheat
//https://qoj.ac/contest/2260/problem/5185
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using pll = pair<ll,ll>;
using ppll = pair<pll,pll>;
ppll Normalize(ppll s){return {min(s.first,s.second),max(s.first,s.second)};}
pll Rotate(pll p){return {p.second,2-p.first};}
ppll Rotate(ppll s){return Normalize({Rotate(s.first),Rotate(s.second)});}
vll dp(7), mask1={1,3,7}, mask2={0,5,10,15}; vector<vector<ppll>> G; map<ppll,pll> ID; bool setupFlag=false;
void Setup(){
if(setupFlag) return;
for(int x1=0; x1<=2; x1++) for(int x2=0; x2<=2; x2++){
for(int y1=0; y1<=2; y1++) for(int y2=0; y2<=2; y2++){
if(pll{x1,y1}<pll{x2,y2} && __gcd(abs(x1-x2),abs(y1-y2))==1){
ppll seg={{x1,y1},{x2,y2}}; vector<ppll> temp;
if(ID.count(seg)) continue;
for(int i=0; i<4; i++) temp.push_back(seg), ID[seg]={(ll)G.size(),i}, seg=Rotate(seg);
G.push_back(temp);
}
}
}
for(int i=0; i<7; i++) dp[i]=3*(1ll<<(4*i))*(1ll<<(12-2*i));
setupFlag=true;
}
vector<pair<pair<int,int>,pair<int,int>>> BuildPattern(int n){
Setup(); n--; ll groupID=-1; vll ans(7,0); vector<pair<pair<int,int>,pair<int,int>>> res;
for(int i=0; i<7; i++){
if(n<dp[i]){groupID=i; break;}
else n-=dp[i];
}
ll x=n%(1ll<<(4*groupID)); n/=(1ll<<(4*groupID)); ans[groupID]=mask1[n%3]; n/=3;
for(int i=0; i<groupID; i++) ans[i]=x%16, x/=16;
for(int i=groupID+1; i<7; i++) ans[i]=mask2[n%4], n/=4;
for(int i=0; i<7; i++) for(int j=0; j<4; j++){
if((ans[i]>>j)&1) res.push_back(G[i][j]);
}
return res;
}
int GetCardNumber(vector<pair<pair<int,int>,pair<int,int>>> P){
Setup(); vll pMask(7,0), ansMask(7,0); map<ll,ll> to={{0,0},{5,1},{10,2},{15,3}};
for(auto &x:P){
x=Normalize(x);
if(ID.count(x)) pMask[ID[x].first]+=(1ll<<ID[x].second);
}
ll ans=0, groupID=-1, a=-1, b=-1;
for(int i=6; i>=0; i--) if(!to.count(pMask[i])){
groupID=i; ll x=pMask[i];
for(int j=0; j<3 && a==-1; j++) for(int k=0; k<4; k++){
if((((mask1[j]<<k)|(mask1[j]>>(4-k)))&15)==pMask[i]){a=j; b=k; break;}
}
break;
}
for(int i=0; i<7; i++) ansMask[i]=(((pMask[i]>>b)|(pMask[i]<<(4-b)))&15);
ll x=0, y=0;
for(int i=0; i<groupID; i++) x+=ansMask[i]*(1ll<<(4*i));
for(int i=groupID+1; i<7; i++) y+=to[ansMask[i]]*(1ll<<(2*(i-groupID-1)));
ans+=x+(1ll<<(4*groupID))*(a+3*y);
for(int i=0; i<groupID; i++) ans+=dp[i];
return ans+1;
}