def solution(n, results):
board = [[-1 for _ in range(n)] for _ in range(n)]
for win, lose in results:
board[win-1][lose-1] = 1
board[lose-1][win-1] = 0
for i in range(n):
for j in range(n):
for k in range(n):
if board[j][i] == -1:
continue
if board[j][i] == board[i][k]: #j > i, i > k -> j > k
board[j][k] = board[j][i]
board[k][j] = not board[j][i]
answer = 0
for i in range(n):
if -1 in board[i][:i] + board[i][i+1:]:
continue
answer += 1
return answer