# Original solution
from collections import deque


def isPalindrome(s):
    L = len(s)
    return all(s[i] == s[L - 1 - i] for i in range(L // 2))


N = int(eval(input()))
S = []
for _ in range(N):
    s, c = input().split()
    S.append((s, int(c)))

path = [dict() for _ in range(2)]
q = deque()
S.sort(key=lambda x: x[1])
for s, c in S:
    if s not in path[0] or path[0][s] > c:
        path[0][s] = c
        q.append((0, s, c))

INF = 10 ** 15
ans = INF
while q:
    side, s0, c0 = q.popleft()
    if isPalindrome(s0):
        ans = min(ans, c0)
    else:
        for s1, c1 in S:
            if side == 0:
                l, r = s0, s1
            else:
                l, r = s1, s0

            L = len(l)
            R = len(r)
            isCandidate = False
            if L > R:
                if all(l[i] == r[-1 - i] for i in range(R)):
                    ns = l[len(r):]
                    nc = c0 + c1
                    nside = 0
                    isCandidate |= True
            elif L < R:
                if all(l[i] == r[-1 - i] for i in range(L)):
                    ns = r[:-len(l)]
                    nc = c0 + c1
                    nside = 1
                    isCandidate |= True
            else:
                if all(l[i] == r[-1 - i] for i in range(L)):
                    ns = ''
                    nc = c0 + c1
                    nside = 0
                    isCandidate |= True
            if isCandidate and (ns not in path[nside] or path[nside][ns] > nc):
                path[nside][ns] = nc
                q.append((nside, ns, nc))
if ans < INF:
    print(ans)
else:
    print((-1))


# Optimized solution
from collections import deque


def isPalindrome(s):
    ''' s: string が回文か判定する. 回文 -> True, 回文でない -> False.
    '''
    L = len(s)
    return all(s[i] == s[L - 1 - i] for i in range(L // 2))


N = int(eval(input()))
S = []
for _ in range(N):
    s, c = input().split()
    S.append((s, int(c)))

path = [dict() for _ in range(2)]  # 余り文字列へ到達するコスト, path[0]:左側余り, path[1]: 右側余り
q = deque()
S.sort(key=lambda x: x[1])  # コストの小さい順にソートしておくと速くなる.
# 初期処理. Siを左側余り文字列と見做してキューに入れていく.
for s, c in S:
    if s not in path[0] or path[0][s] > c:
        path[0][s] = c
        q.append((0, s, c))

INF = 10 ** 15
ans = INF
while q:
    side, s0, c0 = q.popleft()

    if isPalindrome(s0):  # 余り文字列自体が回文のとき
        ans = min(ans, c0)
    else:  # 余り文字列が回文でないとき, 他の文字列を追加する
        for s1, c1 in S:
            if side == 0:
                l, r = s0, s1
            else:
                l, r = s1, s0

            L = len(l)
            R = len(r)
            isCandidate = False  # s1が追加可能な文字列かどうかを示すフラグ
            if L > R:
                if all(l[i] == r[-1 - i] for i in range(R)):
                    ns = l[R:]
                    nc = c0 + c1
                    nside = 0
                    isCandidate |= True
            elif L < R:
                if all(l[i] == r[-1 - i] for i in range(L)):
                    ns = r[:-L]
                    nc = c0 + c1
                    nside = 1
                    isCandidate |= True
            else:
                if all(l[i] == r[-1 - i] for i in range(L)):
                    ns = ''
                    nc = c0 + c1
                    nside = 0
                    isCandidate |= True
            if isCandidate and (ns not in path[nside] or path[nside][ns] > nc):
                path[nside][ns] = nc
                q.append((nside, ns, nc))
if ans < INF:
    print(ans)
else:
    print((-1))