from itertools import combinations

N = int(input())
population = list(map(int, input().split()))
graph = dict()
for i in range(N):
    graph.setdefault(i,[])
    for node in list(map(int, input().split()))[1:]:
        graph[i].append(node-1)

def DoubleDFS(area,n):
    global graph, population
    area1 = list(area)
    area2 = [i for i in [x for x in range(n)] if i not in area1]
    visited = []
    stack = [area1[0]]
    visited.append(area1[0])
    while stack:
        loc = stack.pop()
        if loc in graph.keys():
            for i in graph[loc]:
                if i in area1 and i not in visited:
                    stack.append(i)
                    visited.append(i)
    if set(visited)==set(area1):
        answer = sum([population[x] for x in area1])
        visited.clear()
    else:
        return float('INF')

    stack.append(area2[0])
    visited.append(area2[0])
    
    while stack:
        loc = stack.pop()
        if loc in graph.keys():
            for i in graph[loc]:
                if i in area2 and i not in visited:
                    stack.append(i)
                    visited.append(i)
    if set(visited)==set(area2):
        return abs(answer - sum([population[x] for x in area2]))
    else:
        return float('INF')



answer = float('INF')

for i in range(1,(N//2)+1):
    for area in combinations([x for x in range(N)],i):
        answer = min(answer, DoubleDFS(area,N))
if answer == float('INF'):
    print(-1)
else:
    print(answer)

