macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String {
            bytes
                .by_ref()
                .map(|r| r.unwrap() as char)
                .skip_while(|c| c.is_whitespace())
                .take_while(|c| !c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, [ $t:tt ]) => {
        {
            let len = read_value!($next, usize);
            (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
        }
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_imports)]
use std::cmp::{min, max};

fn main() {
    input!{
        n: usize,
        v: [[usize; 4]; n]
    }
    let mut ps = vec![];
    for p in &v {
        ps.push(canonical(p));
    }
    let mut ans: usize = 0;
    for i in 0..n {
        let f = &ps[i];
        for j in i + 1..n {
            for r in 0..4 {
                let b = rotate(&ps[j], r);
                let mut rest = vec![];
                for k in 0..4 {
                    rest.push(canonical(&vec![f[k], b[(4 - k) % 4], b[3 - k], f[(k + 1) % 4]]));
                }
                rest.sort();
                let mut tot = 1;
                let mut c = 0;
                for k in 0..4 {
                    let d  = if rest[k] == rotate(&rest[k], 1) {
                        4
                    } else if rest[k][0] == rest[k][2] && rest[k][1] == rest[k][3] {
                        2
                    } else {
                        1
                    };
                    let mut t = 0;
                    for l in i + 1..n {
                        if l == j {
                            continue;
                        }
                        if ps[l] == rest[k] {
                            t += 1;
                        }
                    }
                    if k > 0 && rest[k] == rest[k - 1] {
                        if t < c {
                            t = 0;
                        } else {
                            t = (t - c) * d;
                        }
                        c += 1;
                    } else {
                        t *= d;
                        c = 1;
                    }
                    tot *= t;
                }
                ans += tot;
            }
        }
    }
    println!("{}", ans);
}

fn canonical(v: &Vec<usize>) -> Vec<usize> {
    let mut rot = vec![v.clone()];
    for i in 1..v.len() {
        rot.push(rotate(v, i));
    }
    rot.sort();
    rot[0].clone()
}

fn rotate(v: &Vec<usize>, n: usize) -> Vec<usize> {
    v[n..].iter().chain(v[0..n].iter()).cloned().collect()
}


Measured memory_bytes: 4352