use std::cmp::{self, Reverse};
use std::collections::{BinaryHeap, HashMap};

fn main() {
    let n: usize = {
        let mut buf = String::new();
        std::io::stdin().read_line(&mut buf).unwrap();
        buf.trim_end().parse().unwrap()
    };

    let mut ans = std::i64::MAX;

    let mut heap = BinaryHeap::new();
    let mut map = HashMap::new();

    let mut s: Vec<String> = Vec::with_capacity(n);
    let mut c: Vec<i64> = Vec::with_capacity(n);
    for i in 0..n {
        let mut buf = String::new();
        std::io::stdin().read_line(&mut buf).unwrap();
        let mut iter = buf.split_whitespace();
        s.push(iter.next().unwrap().to_string());
        c.push(iter.next().unwrap().parse().unwrap());

        let mut f = |s: &String, c: i64, t: char| {
            let val = map.entry((s.clone(), t)).or_insert(std::i64::MAX);
            if c < *val {
                *val = c;
                heap.push((Reverse(c), (s.clone(), t)));
            }
        };

        if s[i].chars().eq(s[i].chars().rev()) {
            ans = cmp::min(ans, c[i]);
            continue;
        }

        let s0 = s[i].chars().rev().collect::<String>();
        f(&s0, c[i], 'p');
        f(&s0, c[i], 's');
    }

    while let Some((Reverse(c0), (s0, t))) = heap.pop() {
        for i in 0..n {
            let c1 = c0 + c[i];
            if s0.len() == s[i].len() {
                if s0 == s[i] {
                    ans = cmp::min(ans, c1);
                }
                continue;
            }

            let mut f = |s: &String, c: i64, t: char| {
                let val = map.entry((s.clone(), t)).or_insert(std::i64::MAX);
                if c < *val {
                    *val = c;
                    heap.push((Reverse(c), (s.clone(), t)));
                }
            };
        
            if t == 'p' {
                if s0.len() < s[i].len() && s[i].starts_with(&s0)  {
                    let s1 = (&s[i][(s0.len())..]).chars().rev().collect::<String>();
                    if s1.chars().eq(s1.chars().rev()) {
                        ans = cmp::min(ans, c1);
                        continue;
                    }
                    f(&s1, c1, 's');
                }
                if s0.len() > s[i].len() && s0.starts_with(&s[i]) {
                    let s1 = (&s0[(s[i].len())..]).chars().collect::<String>();
                    if s1.chars().eq(s1.chars().rev()) {
                        ans = cmp::min(ans, c1);
                        continue;
                    }
                    f(&s1, c1, 'p');
                }
            }
            if t == 's' {
                if s0.len() < s[i].len() && s[i].ends_with(&s0) {
                    let s1 = (&s[i][..(s[i].len() - s0.len())]).chars().rev().collect::<String>();
                    if s1.chars().eq(s1.chars().rev()) {
                        ans = cmp::min(ans, c1);
                        continue;
                    }
                    f(&s1, c1, 'p');
                }
                if s0.len() > s[i].len() && s0.ends_with(&s[i]) {
                    let s1 = (&s0[..(s0.len() - s[i].len())]).chars().collect::<String>();
                    if s1.chars().eq(s1.chars().rev()) {
                        ans = cmp::min(ans, c1);
                        continue;
                    }
                    f(&s1, c1, 's');
                }
            }
        }
    }
    println!("{}", if ans == std::i64::MAX { -1 } else { ans });
}

Measured memory_bytes: 2228