import java.io.BufferedReader
import java.util.Arrays

fun main(args: Array<String>) {
    /** comment in when you submit **/
    System.`in`.bufferedReader().use { println(getOutputString(it)) }

    /** delete below after your test **/
    // executeTest()
}

private fun getOutputString(reader: BufferedReader): String {
    val num = reader.readLine().split(" ")[0].toInt()

    var pairs = reader.readLines().map {
        val ints = it.split(" ").map { it.toInt() }
        return@map "${ints[0]}${ints[1]}"
    }
    val origPairs = pairs

    while (pairs.size > 0 && pairs.first().length < num) {
        val newPairs = mutableListOf<String>()
        origPairs.forEach { p1 ->
            pairs.forEach { p2 ->
                val candidate = when {
                    p1.first() == p2.last() -> p2 + p1.substring(1)
                    p1.first() == p2.first() -> p2 + p1.reversed().substring(1)
                    p1.last() == p2.last() -> p1 + p2.reversed().substring(1)
                    p1.last() == p2.first() -> p1 + p2.substring(1)
                    else -> null
                }

                candidate?.let {
                    val chars = it.toCharArray()
                    Arrays.sort(chars)
                    for (i in 0 until chars.size - 1) {
                        if (chars[i] == chars[i + 1]) return@let
                    }

                    newPairs.add(it)
                }
            }
        }

        pairs = newPairs.distinct().sortedBy { it.length }
    }

    return pairs.filter { it.startsWith("1") && it.length == num }.size.toString()
}
