#pragma GCC optimize(2)
#include<iostream>
#include <algorithm>
#include <cmath>
#include <iterator>
#include <cstring>
#include <utility>
#include <cstdio>
#include <iomanip>
#include <sstream>
#include <map>
#include <unordered_map>
#include <vector> 
#include <bitset>
#include <cctype>
#include <set>
#include <stack>
#include <queue>
#include <fstream>
#include <numeric>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<string,string> pss;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int MAXN = 3005;
const int MAXM = 50005;
const int MOD = 1e9+7;

#define MP make_pair
#define INF 1e18
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define mst(X,Y) memset(X,Y,sizeof(X)) 
#define rep(i, a, n) for (register int i=(a); i<=(n); i++)
#define per(i, a, n) for (register int i=(a); i>=(n); i--)
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>void debug_out(Head H, Tail... T) { cerr << " " << to_string(H);    debug_out(T...);}
template <typename T> inline T max(T a, T b, T c,T d) { return max(max(a, b),max(c, d)); }
template <typename T> inline T max(T a, T b, T c) { return max(max(a, b),c); }

string s[MAXN];
ll c[MAXN];
map<pss,ll> record;
ll ans = INF;
int n;
inline bool isPalindrome(string a)
{
    string temp = a;
    reverse(temp.begin(),temp.end());
    return a == temp;
}
inline bool isReverseSub(string a,string b)
{   
    reverse(b.begin(),b.end());
    rep (i,0,(int)min(a.size(),b.size())-1)
        if (a[i] != b[i])
            return false;
    return true;
}
void dfs(string l,string r,ll cost)
{
    if (cost >= ans)
    {
        //record[MP(l,r)] = INF;
        return ;
    }
    if (isPalindrome(l) && isPalindrome(r))
    {
        //debug(cost);
        //record[MP(l,r)] = cost;
        ans = min(ans,cost);
        return ;
    }
    if (record[MP(l,r)])
    {
        // ans = min(ans,cost + record[MP(l,r)]);
        return ;
    }
    record[MP(l,r)] = 1;
    if (l == "")
    {
        rep (i,1,n)
        {
            if (isReverseSub(s[i],r))
            {
                string l = s[i];
                string subl,subr;
                if (l.size() > r.size())
                {
                    subl = l.substr(r.size(),l.size()-r.size());
                    subr = "";
                    //ans = min(ans,dfs(subl,subr)+c[i]);
                }   
                else 
                {
                    subl = "";
                    subr = r.substr(0,r.size()-l.size());
                    //ans = min(ans,dfs(subl,subr)+c[i]);
                }
                dfs(subl,subr,cost+c[i]);
            }
        }
    }
    else if (r == "")
    {
        rep (i,1,n)
        {
            if (isReverseSub(l,s[i]))
            {
                string r = s[i];
                string subl,subr;
                if (l.size() > r.size())
                {
                    subl = l.substr(r.size(),l.size()-r.size());
                    subr = "";
                    //ans = min(ans,dfs(subl,subr)+c[i]);
                }   
                else 
                {
                    subl = "";
                    subr = r.substr(0,r.size()-l.size());
                    //ans = min(ans,dfs(subl,subr)+c[i]);
                }
                dfs(subl,subr,cost+c[i]);
            }
        }
    }
    record[MP(l,r)] = 0;
}

int main()
{closeSync;
#ifdef DEBUG
	// freopen("C:/Users/86159/Desktop/a2.in.txt", "r", stdin);
	// freopen("C:/Users/86159/Desktop/output.txt", "w", stdout);
#endif
    
    cin>>n; 
    rep (i,1,n)
        cin>>s[i]>>c[i];
    record[MP("","")] = 0;
    rep (i,1,n)
    {
        //ll temp = dfs(s[i],"") + c[i];
        //debug(temp);
        record.clear();
        dfs(s[i],"",c[i]);
    }
    ll re = ans >= 1e18 ? -1 : ans;
    cout<<re<<endl;
    // debug(isPalindrome("12123"));
    // debug(isReverseSub("abasas","dcba"));
    return 0;
}   

Measured memory_bytes: 3792