#include <bits/stdc++.h>

const int N = 60;
using ll = long long;

struct Node{
    int id, type, pos;

    bool operator <(const Node &n)const{
        if (id != n.id) return id < n.id;
        if (type != n.type) return type < n.type;
        return pos < n.pos;
    }
};

char s[N][N];
int c[N], len[N];

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("%s%d", s[i], &c[i]);
        len[i] = strlen(s[i]);
    }
    using pin = std::pair <ll, Node>;
    std::priority_queue<pin, std::vector<pin>, std::greater<pin>> pq;
    for (int i = 0; i < n; ++ i){
        for (int j = 0; j < len[i]; ++ j){
            int u = j + 1, v = j - 1;
            bool flag = true;
            for ( ; u < len[i] && v >= 0; ++ u, -- v){
                if (s[i][u] != s[i][v]){
                    flag = false;
                    break;
                }
            }
            if (!flag){
                continue;
            }
            if (u == len[i] && v == -1){
                pq.push({c[i], {-1, -1, -1}});
            }
            else if (u == len[i]){
                pq.push({c[i], {i, 1, v}});
            }
            else if (v == -1){
                pq.push({c[i], {i, 0, u}});
            }
            else{
                assert(false);
            }
        }
        for (int j = -1; j < len[i]; ++ j){
            int u = j + 1, v = j;
            bool flag = true;
            for ( ; u < len[i] && v >= 0; ++ u, -- v){
                if (s[i][u] != s[i][v]){
                    flag = false;
                    break;
                }
            }
            if (!flag){
                continue;
            }
            if (u == len[i] && v == -1){
                pq.push({c[i], {-1, -1, -1}});
            }
            else if (u == len[i]){
                pq.push({c[i], {i, 1, v}});
            }
            else if (v == -1){
                pq.push({c[i], {i, 0, u}});
            }
            else{
                assert(false);
            }
        }
    }
    std::map <Node, ll> dis;
    while (!pq.empty()){
        pin u = pq.top();
        pq.pop();
        Node node = u.second;
        if (node.id == -1 && node.type == -1 && node.pos == -1){
            printf("%lld\n", u.first);
            return 0;
        }
        if (dis.count(node)){
            continue;
        }
        dis[node] = u.first;
        int id = node.id;
        if (node.type == 0){
            for (int i = 0; i < n; ++ i){
                int uu = node.pos, v = len[i] - 1;
                bool flag = true;
                for ( ; uu < len[id] && v >= 0; ++ uu, -- v){
                    if (s[id][uu] != s[i][v]){
                        flag = false;
                        break;
                    }
                }
                if (!flag){
                    continue;
                }
                if (uu == len[id] && v == -1){
                    pq.push({u.first + c[i], {-1, -1, -1}});
                }
                else if (uu == len[id]){
                    pq.push({u.first + c[i], {i, 1, v}});
                }
                else if (v == -1){
                    pq.push({u.first + c[i], {id, 0, uu}});
                }
                else{
                    assert(false);
                }
            }
        }
        else{
            for (int i = 0; i < n; ++ i){
                int uu = 0, v = node.pos;
                bool flag = true;
                for ( ; uu < len[i] && v >= 0; ++ uu, -- v){
                    if (s[i][uu] != s[id][v]){
                        flag = false;
                        break;
                    }
                }
                if (!flag){
                    continue;
                }
                if (uu == len[i] && v == -1){
                    pq.push({u.first + c[i], {-1, -1, -1}});
                }
                else if (uu == len[i]){
                    pq.push({u.first + c[i], {id, 1, v}});
                }
                else if (v == -1){
                    pq.push({u.first + c[i], {i, 0, uu}});
                }
                else{
                    assert(false);
                }
            }
        }
    }
    puts("-1");
    return 0;
}

Measured memory_bytes: 3808