#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "functional"
#include "ctime"

using namespace std;

//constexpr long long int MOD = 1000000007;
constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-8;

int N, M, K, H, W, L, R;
//long long int N, M, K, H, W, L, R;

long long int converter(const int a, const int b, const int c, const int d) {
	return ((a * 1000 + b) * 1000 + c) * 1000 + d;
}

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

	cin >> N;
	vector<vector<int>>box(N, vector<int>(4));
	for (auto &i : box)for (auto &j : i)cin >> j;
	long long int ans = 0;
	map<long long int, int>mp;
	map<long long int, int>sz;
	map<long long int, int>oold;
	for (int i = N - 5; i < N; i++) {
		for (int j = 0; j < 4; j++) {
			mp[converter(box[i][j], box[i][(j + 1) % 4], box[i][(j + 2) % 4], box[i][(j + 3) % 4])]++;
			oold[converter(box[i][j], box[i][(j + 1) % 4], box[i][(j + 2) % 4], box[i][(j + 3) % 4])] = i;
			if (mp[converter(box[i][j], box[i][(j + 1) % 4], box[i][(j + 2) % 4], box[i][(j + 3) % 4])] == 1) {
				for (int k = 0; k < 4; k++) {
					if (converter(box[i][j], box[i][(j + 1) % 4], box[i][(j + 2) % 4], box[i][(j + 3) % 4]) == converter(box[i][k], box[i][(k + 1) % 4], box[i][(k + 2) % 4], box[i][(k + 3) % 4]))sz[converter(box[i][j], box[i][(j + 1) % 4], box[i][(j + 2) % 4], box[i][(j + 3) % 4])]++;
				}
			}
		}
	}
	for (int i = N - 6; i >= 0; i--) {
		for (int j = i + 1; j < N; j++) {
			for (int k = 0; k < 4; k++) {
				vector<int>old(4, -1);
				vector<int>num(4, 0);
				vector<int>sum(4, 0);
				for (int n = 0; n < 4; n++) {
					auto it = oold.find(converter(box[i][(1 + n) % 4], box[i][n], box[j][(k + 5 - n) % 4], box[j][(k + 4 - n) % 4]));
					if (it != oold.end()) {
						old[n] = it->second;
						sum[n] = mp[converter(box[i][(1 + n) % 4], box[i][n], box[j][(k + 5 - n) % 4], box[j][(k + 4 - n) % 4])];
						num[n] = sz[converter(box[i][(1 + n) % 4], box[i][n], box[j][(k + 5 - n) % 4], box[j][(k + 4 - n) % 4])];
					}
					for (int m = 0; m < 4; m++) {
						if (box[i][(1 + n) % 4] == box[j][m] && box[i][n] == box[j][(m + 1) % 4] && box[j][(k + 5 - n) % 4] == box[j][(m + 2) % 4] && box[j][(k + 4 - n) % 4] == box[j][(m + 3) % 4]) {
							sum[n]--;
						}
					}
				}
				long long int add = 1;
				for (int l = 0; l < 4; l++) {
					for (int m = 0; m < l; m++) {
						if (old[l] == old[m] && num[l]) {
							sum[l] -= num[l];
						}
					}
					add *= sum[l];
				}
				ans += add;
			}
		}
		for (int j = 0; j < 4; j++) {
			mp[converter(box[i][j], box[i][(j + 1) % 4], box[i][(j + 2) % 4], box[i][(j + 3) % 4])]++;
			oold[converter(box[i][j], box[i][(j + 1) % 4], box[i][(j + 2) % 4], box[i][(j + 3) % 4])] = i;
			if (mp[converter(box[i][j], box[i][(j + 1) % 4], box[i][(j + 2) % 4], box[i][(j + 3) % 4])] == 1) {
				for (int k = 0; k < 4; k++) {
					if (converter(box[i][j], box[i][(j + 1) % 4], box[i][(j + 2) % 4], box[i][(j + 3) % 4]) == converter(box[i][k], box[i][(k + 1) % 4], box[i][(k + 2) % 4], box[i][(k + 3) % 4]))sz[converter(box[i][j], box[i][(j + 1) % 4], box[i][(j + 2) % 4], box[i][(j + 3) % 4])]++;
				}
			}
		}
	}
	cout << ans << endl;
}

Measured memory_bytes: 640