BINGO!

시간 제한5초메모리 제한2048 MB

요약
이벤트 이름이 적힌 n x n 빙고 카드와 강의 중 일어나는 m개의 이벤트가 주어질 때, 처음으로 가로, 세로, 대각선 한 줄이 완성되는 시점을 구한다.
난이도

보통10점 중 4점

유형
해시맵, 시뮬레이션, 배열
정답자
아직 제출이 없습니다

문제

A common activity for students during a lecture is playing BINGO. However, they don't play the regular boring game with the numbered balls. Instead, their BINGO sheets contain in every square an event that might (or might not) happen during a lecture, and once the event happens, this square can be crossed off.

Since you'd also like to pay attention during the lecture while playing this game, you decide to build a program that automatically plays BINGO for you. Given a BINGO sheet with a grid of n×nn \times n squares, and a list of events that happen during the lecture, how many events will pass before you can shout "BINGO!"?

Note that you may shout "BINGO!" whenever you crossed off all squares in any row, column, or diagonal of the grid. The middle square in your grid can always be crossed off for free.

입력

One line containing two integers: one odd integer nn, with 1≤n≤1031 \leq n \leq 10^3, and one integer mm, with 0≤m≤1060 \leq m \leq 10^6. nn lines, each containing nn space-separated events. Event names consist of only alphanumeric characters, and every event only happens at most once. mm lines, with on every line an event that happens during the lecture.

출력

One integer, indicating after how many events you can shout "BINGO!". If you cannot shout "BINGO!" during this lecture, output a sad smiley face: :-(

예제2

  1. 예제 1

    입력
    3 10
    WordMispronounced LoudMic Gaming
    Sleeping FreeLunch Latecomer
    2MinutesSilence TeacherAngry Eating
    MicBreaks
    Sleeping
    SlideshowContainsMistake
    WordMispronounced
    Gaming
    PhoneRings
    Eating
    Latecomer
    TeacherAngry
    2MinutesSilence
    
    예상 출력
    7
    
  2. 예제 2

    입력
    3 5
    EventA EventB EventC
    EventD EventE EventF
    EventG EventH EventI
    EventJ
    EventA
    EventK
    EventB
    EventD
    
    예상 출력
    :-(