/* ACM ICPC Asia 98 Tokyo Problem E: Pipeline Scheduling by Naoyuki Tamura (tamura@kobe-u.ac.jp) */ import java.io.*; import java.util.*; public class E { static boolean dbg = false; static DataInputStream in = new DataInputStream(System.in); static int J = 10; static int M = 5; static int N; static boolean task[][]; static int table[][]; static int best; static void debug(String msg) { if (dbg) System.err.print(msg); } static boolean read() throws Exception { String line = null; debug("# Enter\n"); line = in.readLine(); if (line == null) return false; N = Integer.parseInt(line); if (N == 0) return false; task = new boolean[N][M]; for (int u = 0; u < M; u++) { line = in.readLine(); for (int i = 0; i < N; i++) task[i][u] = (line.charAt(i) == 'X'); } return true; } static boolean canSet(int job, int i0) { for (int i = 0; i < N; i++) for (int u = 0; u < M; u++) if (task[i][u] && table[i0 + i][u] >= 0) return false; return true; } static int set(int job, int i0) { int m = -1; for (int i = 0; i < N; i++) { for (int u = 0; u < M; u++) { if (task[i][u]) { table[i0 + i][u] = job; m = Math.max(m, i0+i); } } } return m + 1; } static void printTable(int len) throws Exception { if (! dbg) return; for (int u = 0; u < M; u++) { debug("# "); for (int i = 0; i < len; i++) { if (table[i][u] < 0) debug("."); else debug(Integer.toString(table[i][u])); } debug("\n"); } } static void solve(int job, int n, int len) throws Exception { if (job == J) { if (len < best) { best = len; debug("# find "+best+"\n"); printTable(best); } } else { for (int i = n; i < best - N; i++) { if (canSet(job, i)) { int l = set(job, i); if (l < best) solve(job+1, i+1, l); set(-1, i); } } } } static void schedule() throws Exception { best = N*J; table = new int[N*J][M]; for (int i = 0; i < table.length; i++) for (int u = 0; u < M; u++) table[i][u] = -1; int len = set(0, 0); solve(1, 1, len); } public static void main(String args[]) { if (args.length > 0) dbg = true; try { while (read()) { schedule(); System.out.println(best); } } catch (Exception e) { e.printStackTrace(); } } }