#include <stdio.h>
#include <assert.h>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
#include <queue>
#include <set>
#include <cmath>
#include <map>
//#include <unordered_map>
//#include <unordered_set>
//#include <boost/container/static_vector.hpp>
//#include <boost/unordered_set.hpp>
//#include <boost/unordered_map.hpp>
//#include <unistd.h>

//#include <cv.h>
//#include <highgui.h>
#include <stdlib.h>
#include <string>

long long pow_mod(long long x, long long n, long long mod_num) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) { res = (res * x) % mod_num; }
        x = (x * x) % mod_num;
        n = (n >> 1);
    }
    return res;
}

//const long long MOD = 1000000007;
const long long MOD = (long long)67280421310721;
const int MAX_N = 500;
int N;
int C[MAX_N][4];

std::vector<int> get_colors(int id, int rot) {
    std::vector<int> ret;
    for (int i = 0; i < 4; i++) {
        ret.push_back(C[id][(i + rot) % 4]);
    }
    return ret;
}

std::vector<int> get_std_colors(int id) {
    std::vector<std::vector<int>> vec;
    for (int i = 0; i < 4; i++) {
        vec.push_back(get_colors(id, i));
    }
    std::sort(vec.begin(), vec.end());
    return vec[0];
}

std::vector<int> get_std_colors(const std::vector<int>& colors) {
    assert(colors.size() == 4);
    std::vector<std::vector<int>> vec;
    for (int r = 0; r < 4; r++) {
        std::vector<int> tmp;
        for (int i = 0; i < 4; i++) {
            tmp.push_back(colors[(i+r)%4]);
        }
        vec.push_back(tmp);
    }
    std::sort(vec.begin(), vec.end());
    return vec[0];
}

int symmetry(const std::vector<int>& colors) {
    assert(colors.size() == 4);
    if (colors[0] == colors[1] && colors[1] == colors[2] && colors[2] == colors[3]) {
        return 4;
    }

    if (colors[0] == colors[2] && colors[1] == colors[3]) {
        return 2;
    }
    return 1;
}

std::vector<std::vector<int>> get_middle(const std::vector<int>& c1, const std::vector<int>& c5) {
    assert(c1.size() == 4);
    assert(c5.size() == 4);
    std::vector<std::vector<int>> ret;
    ret.push_back(std::vector<int>({c1[0], c5[1], c5[0], c1[1]}));
    ret.push_back(std::vector<int>({c1[1], c5[0], c5[3], c1[2]}));
    ret.push_back(std::vector<int>({c1[2], c5[3], c5[2], c1[3]}));
    ret.push_back(std::vector<int>({c1[3], c5[2], c5[1], c1[0]}));
    return ret;
}

bool is_same(const std::vector<int>& c1, const std::vector<int>& c2) {
    assert(c1.size() == 4);
    assert(c2.size() == 4);
    for (int i = 0; i < 4; i++) {
        if (c1[i] != c2[i]) {
            return false;
        }
    }
    return true;
}

long long perm(const long long n, const long long r) {
    long long ret = 1;
    for (int i = 0; i < r; i++) {
        ret = ret * (n - i);
    }
    return ret;
}

std::map<std::vector<int>, int> mp;

int main(int argc, char **argv) {
    std::cin >> N;
    for (int i = 1; i <= N; i++) {
        std::cin >> C[i][0] >> C[i][1] >> C[i][2] >> C[i][3];
    }

    for (int i = 1; i <= N; i++) {
        mp[get_std_colors(i)]++;
    }
    
    long long ret = 0;
    for (int s1 = 1; s1 <= N; s1++) {
        for (int s5 = s1 + 1; s5 <= N; s5++) {
            for (int r5 = 0; r5 < 4; r5++) {
                std::vector<std::vector<int>> middle = get_middle(get_colors(s1, 0), get_colors(s5, r5));
                std::map<std::vector<int>, int> cnt;
                for (auto m : middle) {
                    cnt[get_std_colors(m)]++;
                }
                long long tmp = 1;
                for (auto itr = cnt.begin(); itr != cnt.end(); ++itr) {
                    if (mp.find(itr->first) == mp.end()) {
                        tmp = 0;
                        break;
                    } else {
                        long long cand_num = mp[itr->first];
                        if (is_same(itr->first, get_std_colors(s1))) { cand_num--; }
                        if (is_same(itr->first, get_std_colors(s5))) { cand_num--; }
                        if (cand_num < itr->second) {
                            tmp = 0;
                            break;
                        }
                        //tmp = tmp * cn.perm(cand_num, itr->second) * pow_mod(symmetry(itr->first), itr->second, MOD);
                        tmp = tmp * perm(cand_num, itr->second) * pow_mod(symmetry(itr->first), itr->second, MOD);
                    }
                }
                ret += tmp;
            }
        }
    }

    std::cout << ret / 3 << std::endl;
    
    return 0;
}



Measured memory_bytes: 256