Problem: There was an explorer Henry Nelson traveling all over the world. One day he reached an ancient building. He decided to enter this building for his interest, but its entrance seemed to be locked by a strange security system.

There were some black and white panels placed on a line at equal intervals in front of the entrance, and a strange machine with a cryptogram attached. After a while, he managed to read this cryptogram: this entrance would be unlocked when the panels were rearranged in a certain order, and he needed to use this special machine to change the order of panels.

All he could do with this machine was to swap arbitrary pairs of panels. Each swap could be performed by the following steps:

* move the machine to one panel and mark it;
* move the machine to another panel and again mark it; then
* turn on a special switch of the machine to have the two marked panels swapped.



It was impossible to have more than two panels marked simultaneously. The marks would be erased every time the panels are swapped.

He had to change the order of panels by a number of swaps to enter the building. Unfortunately, however, the machine was so heavy that he didn’t want to move it more than needed. Then which steps could be the best?

Your task is to write a program that finds the minimum cost needed to rearrange the panels, where moving the machine between adjacent panels is defined to require the cost of one. You can arbitrarily choose the initial position of the machine, and don’t have to count the cost for moving the machine to that position.



Input

The input consists of multiple datasets.

Each dataset consists of three lines. The first line contains an integer N, which indicates the number of panels (2 ≤ N ≤ 16). The second and third lines contain N characters each, and describe the initial and final orders of the panels respectively. Each character in these descriptions is either ‘B’ (for black) or ‘W’ (for white) and denotes the color of the panel. The panels of the same color should not be distinguished.

The input is terminated by a line with a single zero.

Output

For each dataset, output the minimum cost on a line. You can assume that there is at least one way to change the order of the panels from the initial one to the final one.

Example

Input

4
WBWB
BWBW
8
WWWWBWBB
WWBBWBWW
0


Output

3
9

Solution (in CPP):

Solution:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 16;
const int MAX_BITS = 1 << MAX_N;

const int INF = 1 << 30;

/* typedef */

struct Stat {
  int d, bits, pos;

  Stat() {}
  Stat(int _d, int _bits, int _pos) { d = _d, bits = _bits, pos = _pos; }
  bool operator<(const Stat& st0) const { return d > st0.d; }
};

/* global variables */

int n;
int dists[MAX_BITS][MAX_N];

/* subroutines */

int str2bits(string str, int n) {
  int bits = 0;
  for (int i = 0; i < n; i++) {
    bits <<= 1;
    if (str[i] == 'B') bits |= 1;
  }
  return bits;
}

/* main */

int main() {
  for (;;) {
    cin >> n;
    if (n == 0) break;

    string sstr, gstr;
    cin >> sstr >> gstr;

    int sbits = str2bits(sstr, n);
    int gbits = str2bits(gstr, n);
    //printf("%x -> %x\n", sbits, gbits);

    int maxbits = 1 << n;
    for (int i = 0; i < maxbits; i++)
      for (int j = 0; j < n; j++)
	dists[i][j] = INF;
    
    priority_queue<Stat> q;

    for (int i = 0; i < n; i++) {
      dists[sbits][i] = 0;
      q.push(Stat(0, sbits, i));
    }

    int mind = INF;
    
    while (! q.empty()) {
      Stat u = q.top();
      q.pop();

      if (u.d != dists[u.bits][u.pos]) continue;
      if (u.bits == gbits) {
	mind = u.d;
	break;
      }

      for (int i0 = 0; i0 < n; i0++) {
	int b0 = (u.bits >> i0) & 1;
	int vc0 = (u.pos > i0) ? u.pos - i0 : i0 - u.pos;
	for (int i1 = 0; i1 < n; i1++) {
	  if (i0 == i1) continue;
	  int b1 = (u.bits >> i1) & 1;
	  if (b0 == b1) continue;

	  int vc1 = (i0 > i1) ? i0 - i1 : i1 - i0;
	  int vbits = (u.bits ^ (1 << i0)) ^ (1 << i1);
	  int vd = u.d + vc0 + vc1;
	  if (dists[vbits][i1] > vd) {
	    dists[vbits][i1] = vd;
	    q.push(Stat(vd, vbits, i1));
	  }
	}
      }
    }

    cout << mind << endl;
  }

  return 0;
}