#include<iostream>
#include<queue>
#include<algorithm>
#include<functional>

using namespace std;

vector<string> task;
const int inf = 1<<22;

struct state{
  int num;
  int pos[10];
  int cost;
  state(){
    cost = 0;
    num = 0;
    fill(pos,pos+10,-1);
  }
  state(int p[10], int n, int c){
    copy(p,p+10,pos);
    cost = c;
    num = n;
  }
  bool operator>(const state &t)const{
    return cost>t.cost;
  }
};

void rev(char M[5][300], const state &t){
  for(int i = 0; i < 10; ++i){
    if( t.pos[i] > -1 ){
      for(int j = 0; j < (int)task.size(); ++j){
        for(int k = 0; k < (int)task[j].size(); ++k){
          if( task[j][k] != '.'){
            M[j][t.pos[i]+k] = task[j][k];
          }
        }
      }
    }
  }
}

bool canPut(char M[5][300], int st, int offset){
  for(int i = 0; i < (int)task.size(); ++i){
    for(int j = 0; j < (int)task[i].size(); ++j){
      if( task[i][j] != '.' && M[i][st+offset+j] != 0 ){
        return false;
      }
    }
  }
  return true;
}

int solve(int n){
  state init;
  init.num = 0;
  init.pos[init.num] = 0;
  init.cost = n;
  int ret = inf;

  priority_queue<state, vector<state>, greater<state> > q;
  int vis[10];
  fill(vis,vis+10,inf);
  
  q.push(init);
  while(!q.empty()){
    state cur = q.top(); q.pop();
    if( cur.num == 9 ){
      return cur.cost;
      ret = min(ret, cur.cost);
    }
    
    int initial = *max_element(cur.pos,cur.pos+10);
    char M[5][300] = {{0}};
    rev(M,cur);
    for(int off = 1; off <= n; ++off){
      if(canPut(M,initial,off)){
        state next = cur;
        next.num++;
        next.pos[next.num] = initial + off;
        next.cost = initial + off + n;
        
        if( vis[next.num] > next.cost ){
          // vis[next.num] = next.cost;
          q.push(next);
        }
      }
    }
  }
  return ret;
}

int main()
{
  int n;
  while(true){
    task.clear();
    cin >> n;
    if(n == 0) break;
    for(int i = 0; i < 5; ++i){
      string s;
      cin >> s;
      task.push_back(s);
    }
    cout << solve(n) << endl;
  }
  return 0;
}