{-# LANGUAGE OverloadedStrings, OverloadedLists, UnboxedTuples, TemplateHaskell #-}

import Data.List
import Data.List.Split
import qualified Data.Map as M

main = do
    --a <- readFile "in"
    a <- getContents

    let aa = splitOn "\n" a

    -- orders
    let a1 = head aa
    let a2 = splitOn " " $ head $ tail aa
    let b1 = head $ tail $ tail $ init aa
    let b2 = splitOn " " $ head $ tail $ tail $ tail $ init aa

    let i = concatMap (\x -> map (\y -> if x == y then (show y) else "") b2) a2
    let a = foldl' aaa M.empty i

    --print a1
    --print a2
    --print b1
    --print b2
    let aa = M.delete "" a
    print $ M.size aa

    return ()

aaa :: M.Map String String -> String -> M.Map String String
aaa m s = M.insert s "" m

