import std.stdio;
import std.conv, std.array, std.algorithm, std.string;
import std.math, std.random, std.range, std.datetime;
import std.bigint;

class Face{
	int[] colors;
	int[][] colorAlts;
	ulong count;
	int weight;
	string key;
	this(int[] x){
		count = 1;
		colors = x.toPreKey;
		key = colors.toKey;
		weight = key.weight;
		if(weight == 4) colorAlts = [colors];
		if(weight == 2) colorAlts = [colors, colors[1 .. 4] ~ colors[0 .. 1]];
		if(weight == 1) colorAlts = [colors, colors[1 .. 4] ~ colors[0 .. 1],
				colors[2 .. 4] ~ colors[0 .. 2], colors[3 .. 4] ~ colors[0 .. 3]];
		}
	debug string toString(){
		return weight.to!string ~ " [" ~ key ~ "] " ~ count.to!string;
		}
	}
Face[string] cset;

void main(){
	int n = readln.chomp.to!int;
	
	foreach(i; 0 .. n){
		int[] x = readln.chomp.split.map!(to!int).array;
		string k = x.toKey;
		if(k in cset) cset[k].count += 1;
		else cset[k] = new Face(x);
		}
	
	debug cset.values.map!(f => f.toString).join("\n").writeln;
	
	cset.rehash;
	
	ulong ans = 0;
	foreach(a; cset.values){
		foreach(b; cset.values){
			foreach(cs; b.colorAlts){
				ans += fn(a.colors, cs, a, b);
				}
			}
		}
	
	ans /= 6;
	ans.writeln;
	
	}

int[] toPreKey(int[] x){
	while(x[0] > x[1] || x[0] > x[2] || x[0] > x[3]) x = x[1 .. $] ~ x[0];
	if(x[0] == x[3]) x = x[3] ~ x[0 .. 3];
	if(x[0] == x[3]) x = x[3] ~ x[0 .. 3];
	if(x[0] == x[3]) x = x[3] ~ x[0 .. 3];
	if(x[0] == x[2] && x[1] > x[3]) x = x[2 .. 4] ~ x[0 .. 2];
	return x;
	}
string toKey(int[] x){
	return x.toPreKey.map!(to!string).join(" ");
	}

int weight(string k){
	int[] x = k.split.map!(to!int).array;
	if(x[0] == x[2] && x[1] == x[3]){
		if(x[0] == x[1]) return 4; else return 2;
		}
	else return 1;
	}

ulong fn(int[] x, int[] y, Face a, Face b){
	string[] ks = [
		[x[0], y[0], y[3], x[1]].toKey,
		[x[1], y[3], y[2], x[2]].toKey,
		[x[2], y[2], y[1], x[3]].toKey,
		[x[3], y[1], y[0], x[0]].toKey
		];
	foreach(k; ks) if(!(k in cset)) return 0;
	Face[] fs = [a, b] ~ ks.map!(k => cset[k]).array;
	
	ulong[] ns = fs.map!(f => f.count).array;
	
	foreach(i, f; fs) foreach(ii, ff; fs){
		if(i < ii) if(f.key == ff.key){
			ns[ii] -= 1;
			if(ns[ii] <= 0) return 0;
			}
		}
	
	ulong ans = 1;
	foreach(i, f; fs) ans *= ns[i] * f.weight;
	ans /= fs[0].weight;
	
	debug if(ans > 0){
		writeln(x, " ", y);
		writeln(ns, " ", ans);
		}
	
	return ans;
	
	}