// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools

#include <bits/stdc++.h>

using namespace std;

using i64 = long long;

const i64 MOD = 1e9 + 7;
const i64 INF = i64(1e18) + 7;


template <typename T>
bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

template <typename T>
bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}


// doc: https://shibh308.github.io/library/library/lib/classes/modint.cpp.html
template <i64 mod = MOD>
struct ModInt{
    i64 p;

    ModInt() : p(0){}
    ModInt(i64 x){p = x >= 0 ? x % mod : x + (-x + mod - 1) / mod * mod;}

    ModInt& operator+=(const ModInt& y){p = p + *y - ((p + *y) >= mod ? mod : 0); return *this;}
    ModInt& operator-=(const ModInt& y){p = p - *y + (p - *y < 0 ? mod : 0); return *this;}
    ModInt& operator*=(const ModInt& y){p = (p * *y) % mod; return *this;}
    ModInt& operator%=(const ModInt& y){if(y)p %= *y; return *this;}

    ModInt operator+(const ModInt& y) const{ModInt x = *this; return x += y;}
    ModInt operator-(const ModInt& y) const{ModInt x = *this; return x -= y;}
    ModInt operator*(const ModInt& y) const{ModInt x = *this; return x *= y;}
    ModInt operator%(const ModInt& y) const{ModInt x = *this; return x %= y;}

    friend ostream& operator<<(ostream& stream, const ModInt<mod>& x){
        stream << *x;
        return stream;
    }

    friend ostream& operator>>(ostream& stream, const ModInt<mod>& x){
        stream >> *x;
        return stream;
    }

    ModInt& operator++(){p = (p + 1) % mod; return *this;}
    ModInt& operator--(){p = (p - 1 + mod) % mod; return *this;}

    bool operator==(const ModInt& y) const{return p == *y;}
    bool operator!=(const ModInt& y) const{return p != *y;}

    const i64& operator*() const{return p;}
    i64& operator*(){return p;}

};

using mint = ModInt<>;

// doc: https://shibh308.github.io/library/library/lib/functions/modpow.cpp.html
auto mpow = [](auto x, i64 y){
    auto z = x;
    decltype(x) val = y & 1 ? x : decltype(x)(1);
    while(z *= z, y >>= 1)
        if(y & 1)
            val *= z;
    return val;
};


void solve(long long n, std::vector<std::string> s, std::vector<long long> c){
    vector<string> s_rev;
    for(int i = 0; i < n; ++i){
        auto t = s[i];
        reverse(t.begin(), t.end());
        s_rev.emplace_back(t);
    }
    vector<map<string, i64>> dp(2);

    priority_queue<tuple<i64,int,string>, vector<tuple<i64,int,string>>, greater<tuple<i64,int,string>>> que;
    auto chmi = [&](int x, string y, i64 z){
        if(dp[x].find(y) == dp[x].end())
            dp[x][y] = INF;
        chmin(dp[x][y], z);
        que.emplace(z, x, y);
    };

    for(int i = 0; i < n; ++i){
        for(int j = 0; j <= s[i].size(); ++j){
            string t, rev_t;
            t = rev_t = s[i].substr(0, j);
            reverse(rev_t.begin(), rev_t.end());
            if(t == rev_t){
                chmi(0, s[i].substr(j), c[i]);
            }
        }
        for(int j = 0; j <= s[i].size(); ++j){
            string t, rev_t;
            t = rev_t = s[i].substr(j);
            reverse(rev_t.begin(), rev_t.end());
            if(t == rev_t){
                string tv = s[i].substr(0, j);
                reverse(tv.begin(), tv.end());
                chmi(1, tv, c[i]);
            }
        }
    }

    auto f = [&](int i, int fl, int len){
        if(!fl)
            return s[i].substr(0, len);
        else
            return s_rev[i].substr(0, len);
    };

    while(!que.empty()){
        i64 dist;
        int side;
        string str;
        tie(dist, side, str) = que.top();
        que.pop();
        if(dist != dp[side][str])
            continue;
        for(int i = 0; i < n; ++i){
            int siz = s[i].size();
            if(siz > str.size())
                continue;
            string target = str.substr(0, siz);
            if(s[i].size() == siz && f(i, side ^ 1, siz) == target){
                chmi(side, str.substr(siz), dist + c[i]);
            }
        }
        for(int i = 0; i < n; ++i){
            if(f(i, side ^ 1, str.size()) == str){
                string target = f(i, side ^ 1, s[i].size()).substr(str.size());
                chmi(side ^ 1, target, dist + c[i]);
            }
        }
    }
    i64 ans = INF;
    chmin(ans, dp[0][""] == 0 ? INF : dp[0][""]);
    chmin(ans, dp[1][""] == 0 ? INF : dp[1][""]);
    cout << (ans == INF ? -1 : ans) << endl;
}


signed main(){
    long long N;
    scanf("%lld",&N);
    std::vector<std::string> S(N);
    std::vector<long long> C(N);
    for(int i = 0 ; i < N ; i++){
        std::cin >> S[i];
        scanf("%lld",&C[i]);
    }
    solve(N, std::move(S), std::move(C));
    return 0;
}


Measured memory_bytes: 3992