extern crate core;

use std::fmt;
use std::cmp::{Ordering, min, max};
use std::f32::MAX;
use std::ops::{Add, Sub, Mul, Div, Neg, Index, IndexMut, SubAssign, Range};
use std::collections::{BTreeMap, VecDeque, BinaryHeap, BTreeSet, HashMap};
use std::fmt::Display;

fn show<T: Display>(vec: &Vec<T>) {
    if vec.is_empty() {
        println!("[]");
    }else {
        print!("[{}", vec[0]);
        for i in 1 .. vec.len() {
            print!(", {}", vec[i]);
        }
        println!("]");
    }
}
fn show2<T: Display>(vec: &Vec<Vec<T>>) {
    if vec.is_empty() {
        println!("[]");
    }else {
        for l in vec {
            show(l);
        }
    }
}

macro_rules! read_line{
    () => {{
        let mut line = String::new();
        std::io::stdin().read_line(&mut line).ok();
        line
    }};
    (delimiter: ' ') => {
        read_line!().split_whitespace().map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (delimiter: $p:expr) => {
        read_line!().split($p).map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (' ') => {
        read_line!(delimiter: ' ')
    };
    ($delimiter:expr) => {
        read_line!(delimiter: $delimiter)
    };
    (' '; $ty:ty) => {
        read_line!().split_whitespace().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
    ($delimiter:expr; $ty:ty) => {
        read_line!($delimiter).into_iter().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
}
macro_rules! read_value{
    () => {
        read_line!().trim().parse().ok().unwrap()
    }
}
macro_rules! let_all {
    ($($n:ident:$t:ty),*) => {
        let line = read_line!(delimiter: ' ');
        let mut iter = line.iter();
        $(let $n:$t = iter.next().unwrap().parse().ok().unwrap();)*
    };
}

macro_rules! let_mut_all {
    ($($n:ident:$t:ty),*) => {
        let line = read_line!(delimiter: ' ');
        let mut iter = line.iter();
        $(let mut $n:$t = iter.next().unwrap().parse().ok().unwrap();)*
    };
}
#[derive(Copy, Clone)]
struct BitSet {
    mask: i32
}
impl BitSet {
    fn is_on(&self, value: i32) -> bool {
        self.mask & (1 << value) != 0
    }
    fn set(&self, value: i32) -> BitSet {
        BitSet{mask: self.mask | (1 << value)}
    }
    fn to_index(&self) -> usize {
        self.mask as usize
    }
}
fn abs(value: i32) -> i32 {
    if value >= 0 {value}else {-value}
}
fn solve(current: usize, flag: BitSet, from: &Vec<char>, to: &Vec<char>, memo: &mut Vec<Vec<i32>>) -> i32 {
    if memo[current][flag.to_index()] >= 0 {
        memo[current][flag.to_index()]
    }else {
        let mut result = std::i32::MAX;
        for i in 0 .. from.len() {
            if !flag.is_on(i as i32) && from[i] != to[i] {
                for j in 0 .. from.len() {
                    if i != j && !flag.is_on(j as i32) && from[i] == to[j] && to[i] == from[j] {
                        let temp = abs(i as i32 - current as i32) + abs(i as i32 - j as i32) + solve(j, flag.set(i as i32).set(j as i32), from, to, memo);
                        if result > temp {
                            result = temp;
                        }
                    }
                }
            }
        }
        memo[current][flag.to_index()] = result;
        result
    }
}
fn main() {
    loop {
        let_all!(n: usize);
        if n == 0 {return;}
        let from = read_line!().trim().chars().collect::<Vec<_>>();
        let to = read_line!().trim().chars().collect::<Vec<_>>();
        let mut flag = BitSet{mask: 0};
        for i in 0 .. n {
            if from[i] == to[i] {
                flag = flag.set(i as i32);
            }
        }
        let mut memo = vec![vec![-1; 1 << n]; n];
        for i in 0 .. n {
            memo[i][(1 << n) - 1] = 0;
        }
        let mut min_value = std::i32::MAX;
        for i in 0 .. n {
            if !flag.is_on(i as i32) {
                let temp = solve(i, flag, &from, &to, &mut memo);
                if min_value > temp {
                    min_value = temp;
                }
            }
        }
        println!("{}", min_value);
    }
}



Measured memory_bytes: 10708