#include "bits/stdc++.h"
using namespace std;

typedef long long li;

li hash_color(const vector<int> &colors) {
    li v = 0;
    li pow = 1;
    for (int i = 0; i < 4; ++i) {
        v += colors[i] * pow;
        pow *= 1000;
    }
    return v;
}

vector<int> rotate(const vector<int> &colors) {
    return {colors[1], colors[2], colors[3], colors[0]};
}

vector<int> rotate(const vector<int> &colors, const int count) {
    vector<int> ret = colors;
    for (int i = 0; i < count; ++i) {
        ret = rotate(ret);
    }
    return ret;
}

li normalized_hash(const vector<int> &colors) {
    vector<int> c = colors;
    li h = hash_color(c);
    for (int i = 0; i < 3; ++i) {
        c = rotate(c);
        h = min(h, hash_color(c));
    }
    return h;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;
//    n = 400;
    vector<vector<int>> colors(n, vector<int>(4));
    map<li, li> card_counts;

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < 4; ++j) {
            cin >> colors[i][j];
        }
        card_counts[normalized_hash(colors[i])] += 1;
    }

    li ans = 0;
    for (int bottom = 0; bottom < n; ++bottom) {
        const li bottom_hash = normalized_hash(colors[bottom]);
//        cerr << bottom << endl;
        // flipped
        const vector<int> bottom_colors = {
                colors[bottom][3],
                colors[bottom][2],
                colors[bottom][1],
                colors[bottom][0]
        };
        for (int top = bottom + 1; top < n; ++top) {
            const li top_hash = normalized_hash(colors[top]);

            for (int top_dir = 0; top_dir < 4; ++top_dir) {
                const vector<int> top_colors = rotate(colors[top], top_dir);

                li subans = 1;
                map<li, int> used_hash;
                for (int side_index = 0; side_index < 4; ++side_index) {
                    const vector<int> side_colors = {
                            bottom_colors[side_index],
                            bottom_colors[(side_index + 1) % 4],
                            top_colors[(side_index + 1) % 4],
                            top_colors[side_index]
                    };
                    if (
                            side_colors[0] == side_colors[1] &&
                            side_colors[1] == side_colors[2] &&
                            side_colors[2] == side_colors[3] &&
                            side_colors[3] == side_colors[0]
                    ) {
                        subans *= 4;
                    } else if (
                            side_colors[0] == side_colors[2] &&
                            side_colors[1] == side_colors[3]
                    ) {
                        subans *= 2;
                    } else {
                        subans *= 1;
                    }

                    const li side_hash = normalized_hash(side_colors);
                    subans *= (card_counts[side_hash] - used_hash[side_hash] - (side_hash == bottom_hash) - (side_hash == top_hash));
                    used_hash[side_hash]++;
                }

                ans += subans;
            }
        }
    }
    cout << ans / 3 << endl;
    return 0;
}

Measured memory_bytes: 80128