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))

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))

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))

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))

-# S.sort(key=lambda x: x[1]) +S.sort(key=lambda x: x[1])