#include <iostream>
#include <string>
using namespace std;

long process(int m, string pre, string post)
{
	if (pre.substr(0,1) != post.substr(post.length()-1,1))
		return 0;
	if (pre.length() == 1)
		return 1;
	long ans = 1;
	pre = pre.substr(1, pre.length());
	post = post.substr(0, post.length()-1);
	int numchild = 0;
	long comb = 1;
	while (pre.length() > 0) {
		numchild++;
		int index = post.find(pre[0]);
		if (index == -1) {
//			cout << "Ooops, this should never happen: " << pre << ',' << post << endl;
//			exit(-1);
			return 0;
		}
		string newPost = post.substr(0, index+1);
		post = post.substr(index+1, post.length());
		string newPre = pre.substr(0, index+1);
		pre = pre.substr(index+1, pre.length());
		ans *= process(m, newPre, newPost);
		if (ans == 0)
			return 0;
		comb = comb*(m-numchild+1)/numchild;
	}
	return ans*comb;
}


int main()
{
	string post, pre;
	int  m;

	cin >> m;
	while (m != 0) {
		cin >> pre >> post;
		if (pre.length() != post.length())
			cout << 0 << endl;
		else
			cout << process(m, pre, post) << endl;
		cin >> m;
	}
	return 0;
}