#include <bits/stdc++.h>

#define DEBUG 1

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned int uint;
typedef unsigned short ushort;
typedef pair<int, int> PII;

#define MAX_INT (int)0x7fffffff
#define MIN_INT (int)0x80000000
#define MAX_UINT (uint)0xffffffff

#define TTi template<typename T> inline
TTi T SQR(T x) { return x * x; }

#define CONCAT3_NX(x, y, z) x ## y ## z
#define CONCAT3(x, y, z) CONCAT3_NX(x, y, z)
#define VAR(name) CONCAT3(__tmpvar__, name, __LINE__)
#define TYPE(x) __typeof(x)

#define FOR(i, s, n)  for (TYPE(n) i=(s),   VAR(end)=(n);  i <  VAR(end);  i++)
#define RFOR(i, s, n) for (TYPE(n) i=(n)-1, VAR(end)=(s);  i >= VAR(end);  i--)
#define FORN(i, n)    FOR(i, 0, n)
#define RFORN(i, n)   RFOR(i, 0, n)
#define FOREACH(i, v) for (auto& i: v)

#define SC() scanf("\n")
#define SC1(fmt, a) scanf(fmt, &a)
#define SC2(fmt, a, b) scanf(fmt, &a, &b)
#define SC3(fmt, a, b, c) scanf(fmt, &a, &b, &c)
#define SCi(a) scanf("%d", &a)
#define SCii(a,b) scanf("%d%d", &a, &b)
#define SCiii(a,b,c) scanf("%d%d%d", &a, &b, &c)
#define fLL "%lld"
#define SCl(a) scanf(fLL, &a)
#define SCll(a,b) scanf(fLL fLL, &a, &b)
#define SClll(a,b,c) scanf(fLL fLL fLL, &a, &b, &c)
#define SCs(s, n) {scanf("%s", s); n = strlen(s);}
#define SCc(s) scanf("%c", &c)

#define MP make_pair
#define PB push_back
#define WHOLE(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define POPST(stack) (stack).top();(stack).pop();
#define POPQ(queue) (queue).front();(queue).pop();
#define CONTAINS(v, x) (find(WHOLE(v), (x)) != v.end())
#define SORT(v) (sort(WHOLE(v)))

#define LIMIT(x, lim) {if (x > lim) x = lim;}
TTi void UPDATE_MIN(T &x, T y) {if (y < x) {x = y;}}
TTi void UPDATE_MAX(T &x, T y) {if (x < y) {x = y;}}
TTi int ARGMAX(T cont) { return max_element(cont.begin(), cont.end()) - cont.begin(); }
TTi int ARGMIN(T cont) { return min_element(cont.begin(), cont.end()) - cont.begin(); }

TTi int hamming(T x) {return __builtin_popcountll((long long)x);}
int hamming(int x) {return __builtin_popcount(x);}
int hamming(long x) {return __builtin_popcountl(x);}
int hamming(long long x) {return __builtin_popcountll(x);}

vector<string> split(const string& s, char c) {
    vector<string> v; stringstream ss(s); string x;
    while (getline(ss, x, c)) v.emplace_back(x); return move(v);
}
template<typename T, typename... Args>
inline string arrStr(T arr, int n) {
    stringstream s; s << "[";
    FORN(i, n - 1) s << arr[i] << ",";
    s << arr[n - 1] << "]";
    return s.str();
}

// #ifndef ONLINE_JUDGE
#ifdef JUDGE_LOCAL
    #define EPR(args...)   if (DEBUG) {fprintf(stderr, args);}
    #define EARR(arr, n)   if (DEBUG) {FORN(i, n) fprintf(stderr, "%d, ", arr[i]);}
    #define EVEC(arr)      if (DEBUG) {FORN(i, arr.size()) fprintf(stderr, "%d, ", arr[i]);}
    #define EVARS(args...) if (DEBUG) { __evars_begin(__LINE__); __evars(split(#args, ',').begin(), args);}

    inline void __evars_begin(int line) { cerr << "#" << line << ": "; }
    inline void __evars(vector<string>::iterator it) { cerr << endl; }
    TTi void __evars_out_var(vector<T> val) { cerr << arrStr(val, val.size()); }
    TTi void __evars_out_var(T* val) { cerr << arrStr(val, 10); }
    TTi void __evars_out_var(T val) { cerr << val; }
    template<typename T, typename... Args>
    inline void __evars(vector<string>::iterator it, T a, Args... args) {
        cerr << it->substr((*it)[0] == ' ', it->length()) << "=";
        __evars_out_var(a);
        cerr << "; ";
        __evars(++it, args...);
    }
#else
    #define EPR(args...) 1
    #define EARR(args...) 1
    #define EVEC(args...) 1
    #define EVARS(args...) 1
#endif

template<class T> inline string TOSTR(const T & x) { stringstream ss; ss << x; return ss.str(); }
#define DIE(args...) {printf(args);exit(0);}
inline void PR(void) {}
inline void PR(int x) {printf("%d", x);}
inline void PR(LL x) {printf("%lld", x);}
inline void PR(size_t x) {printf("%llu", (ULL)x);}
inline void PR(const char * s) {printf("%s", s);}
inline void PR(double f) {printf("%.10f", f);}
inline void PR(long double f) {printf("%.10f", (double)f);}
TTi void PR(vector<T> &vec) {auto sz = vec.size();for(auto x:vec){PR(x);(--sz)?putc(0x20,stdout):0;}}
TTi void PRS(T x) {PR(x);putc(0x20,stdout);}
TTi void PRN(T x) {PR(x);putc(0x0a,stdout);}
void PRN(void) {putc(0x0a,stdout);}

struct pairhash {
    template <typename T, typename U>
    std::size_t operator() (const std::pair<T, U> &x) const {
        return std::hash<T>()(x.first) ^ std::hash<U>()(x.second);
    }
};

const int MOD = 1000 * 1000 * 1000 + 7;
const double PI = 3.1415926535897932384626433832795l;

TTi T gcd(T a, T b) {
    return a ? gcd(b % a, a) : b;
}

inline void addto(int &a, int b) {
    a += b;
    if (a >= MOD) a -= MOD;
}
inline int add(int a, int b) {
    a += b;
    if (a >= MOD) a -= MOD;
    return a;
}
inline void subto(int &a, int b) {
    a -= b;
    if (a < 0) a += MOD;
    if (a >= MOD) a -= MOD;
}
inline int sub(int a, int b) {
    a -= b;
    if (a < 0) a += MOD;
    if (a >= MOD) a -= MOD;
    return a;
}
inline void multo(int &a, int b) {
    a = (long long)a * b % MOD;
}
inline int mul(int a, int b) {
    return (long long)a * b % MOD;
}
inline int mulmod(int a, int b, int mod) {
    return (long long)a * b % mod;
}
inline int powmod(int a, int e, int mod) {
    int x;
    for(x = 1; e > 0; e >>= 1) {
        if (e & 1)
            x = mulmod(x, a, mod);
        a = mulmod(a, a, mod);
    }
    return x;
}
inline int invmod_prime(int a, int mod) {
    return powmod(a, mod - 2, mod);
}
inline LL invmod_LL(LL p){
    LL q = p;
    for(LL a = p*p; a != 1; a*=a) q*=a;
    return q;
}


// -----------------------------------------------------------------
// CODE
// -----------------------------------------------------------------


int N, M, K, L, E, Q;

struct Colors {
    int colors[4];
    inline void normalize() {
        int i = (less(0, 1)) ? 0 : 1;
        int j = (less(2, 3)) ? 2 : 3;
        int ans = (less(i, j)) ? i : j;
        rotate_left(ans);
    }
    inline void rotate_left(int num) {
        int tmp[4];
        FORN(i, 4) tmp[i] = colors[(num+i) & 3];
        FORN(i, 4) colors[i] = tmp[i];
    }
    inline int mult() const {
        int ans = 1;
        FOR(i, 1, 4)
            if ((!less(0, i)) && (!less(i, 0)))
                ans++;
        return ans;
    }
    inline bool less(int i, int j) const {
        FORN(k, 4) {
            if (colors[(i+k)&3] < colors[(j+k)&3]) return true;
            if (colors[(i+k)&3] > colors[(j+k)&3]) return false;
        }
        return false;
    }
    inline bool operator<(const Colors &other) const {
        FORN(k, 4) {
            if (colors[k] < other.colors[k]) return true;
            if (colors[k] > other.colors[k]) return false;
        }
        return false;
    }
    inline bool operator==(const Colors &other) const {
        return (!(*this < other)) && (!(other < *this));
    }
    inline Colors pair(const Colors &other, int offset) const {
        Colors ans;
        if (offset == 0) {
            ans.colors[0] = colors[0];
            ans.colors[1] = other.colors[1];
            ans.colors[2] = other.colors[0];
            ans.colors[3] = colors[1];
        }
        else if (offset == 1) {
            ans.colors[0] = colors[1];
            ans.colors[1] = other.colors[0];
            ans.colors[2] = other.colors[3];
            ans.colors[3] = colors[2];
        }
        else if (offset == 2) {
            ans.colors[0] = colors[2];
            ans.colors[1] = other.colors[3];
            ans.colors[2] = other.colors[2];
            ans.colors[3] = colors[3];
        }
        else if (offset == 3) {
            ans.colors[0] = colors[3];
            ans.colors[1] = other.colors[2];
            ans.colors[2] = other.colors[1];
            ans.colors[3] = colors[0];
        }
        else {
            assert(0);
        }
        ans.normalize();
        return ans;
    }
};

map<Colors, int> colcnt;
vector<Colors> tiles;

LL countfit(Colors &a, Colors &b, Colors &brot) {
    Colors dst[4] = {
        a.pair(brot, 0),
        a.pair(brot, 1),
        a.pair(brot, 2),
        a.pair(brot, 3),
    };
    LL ans = 1;
    FORN(i, 4) {
        auto &c = dst[i];
        if (colcnt.find(c) == colcnt.end()) break;
        ans *= 1ll * colcnt[c] * c.mult();
        colcnt[c]--;
    }
    FORN(i, 4) {
        auto &c = dst[i];
        if (colcnt.find(c) == colcnt.end()) return 0;
        colcnt[c]++;
    }
    return ans;
}

LL countSmallest(Colors a) {
    LL ans = 0;
    for(auto &pb: colcnt) {
        if (!pb.second) continue;
        Colors b = pb.first;
        Colors brot = pb.first;
        LL multi = pb.second;
        pb.second--;
        FORN(rot, 4) {
            ans += multi * countfit(a, b, brot);
            brot.rotate_left(1);
        }
        pb.second++;
    }
    return ans;
}

int main() {
    ios_base::sync_with_stdio(0);

    SCi(N);
    FORN(i, N) {
        Colors c;
        FORN(j, 4)
            SCi(c.colors[j]);
        c.normalize();
        colcnt[c]++;
        tiles.push_back(c);
    }

    LL ans = 0;
    FORN(i, (int)tiles.size()) {
        Colors a = tiles[i];
        colcnt[a]--;
        ans += countSmallest(a);
    }
    PRN(ans);
    return 0;
}


Measured memory_bytes: 256