def dfs(si, nowface):
    global link, visited
    result = True
    visited[si] = True
    if (nowface[1] != si):
        return False
    for dir in range(1, 5):
        if (link[si][dir] and (not visited[link[si][dir]])):
            face = turn(nowface, dir)
            result = (result and dfs(link[si][dir], face))
    return result
def dfs(si, nowface):
    global link, visited
    result = True
    visited[si] = True
    if (nowface[1] != si):
        return False
    for dir in range(1, 5):
        if (link[si][dir] and (not visited[link[si][dir]])):
            face = turn(nowface, dir)
            result = (result and dfs(link[si][dir], face))
    return result