After overcoming the stairs Dasha came to classes. She needed to write a password to begin her classes. The password is a string of length n which satisfies the following requirements:  There is at least one digit in the string,  There is at least one lowercase (small) letter of the Latin alphabet in the string,  There is at least one of three listed symbols in the string: '#', '*', '&'.  [Image] 

Considering that these are programming classes it is not easy to write the password.

For each character of the password we have a fixed string of length m, on each of these n strings there is a pointer on some character. The i-th character displayed on the screen is the pointed character in the i-th string. Initially, all pointers are on characters with indexes 1 in the corresponding strings (all positions are numbered starting from one).

During one operation Dasha can move a pointer in one string one character to the left or to the right. Strings are cyclic, it means that when we move the pointer which is on the character with index 1 to the left, it moves to the character with the index m, and when we move it to the right from the position m it moves to the position 1.

You need to determine the minimum number of operations necessary to make the string displayed on the screen a valid password. 


-----Input-----

The first line contains two integers n, m (3 ≤ n ≤ 50, 1 ≤ m ≤ 50) — the length of the password and the length of strings which are assigned to password symbols. 

Each of the next n lines contains the string which is assigned to the i-th symbol of the password string. Its length is m, it consists of digits, lowercase English letters, and characters '#', '*' or '&'.

You have such input data that you can always get a valid password.


-----Output-----

Print one integer — the minimum number of operations which is necessary to make the string, which is displayed on the screen, a valid password. 


-----Examples-----
Input
3 4
1**2
a3*0
c4**

Output
1

Input
5 5
#*&#*
*a1c&
&q2w*
#a3c#
*&#*&

Output
3



-----Note-----

In the first test it is necessary to move the pointer of the third string to one left to get the optimal answer.  [Image] 

In the second test one of possible algorithms will be:   to move the pointer of the second symbol once to the right.  to move the pointer of the third symbol twice to the right.  [Image]

Difficulty: interview

Starter code:

Solutions:
import math, re, itertools as it;prime = lambda n: len([i for i in range(2, int(math.sqrt(n) + 1)) if n % i == 0]) == 0;gcd = lambda a, b: gcd(b, a % b) if b else a;fact = lambda x: x * fact(x - 1) if x else 1;bino = lambda n, k: fact(n) / fact(k) / fact(n - k);fib11 = lambda n: 1 if n < 2 else fib11(n - 1) + fib11(n - 2);fib01 = lambda n: 0 if n == 0 else 1 if n == 1 else fib01(n - 1) + fib01(n - 2);sumofd = lambda x: x if x < 10 else sumofd(x // 10) + x % 10

n, m = map(int, input().split(' '))
a = []
dp = []
sc = st = sl = 1000000
for _ in range(n):
	a.append(input())
	c = t = l = 1000000
	for i in range(len(a[-1])):
		if a[-1][i] in '0123456789':
			c = min(c, i, len(a[-1]) - i)
		if a[-1][i] in '#*&':
			t = min(t, i, len(a[-1]) - i)
		if 'a' <= a[-1][i] <= 'z':
			l = min(l, i, len(a[-1]) - i)
	'''if c == t == 1000000 or c == l == 1000000 or l == t == 1000000:
		if c == t == 1000000:
			sl = 0
		if c == l == 1000000:
			st = 0
		if l == t == 1000000:
			sc = 0
		continue'''
	dp.append([c, t, l])
mm = 1000000
kk = it.permutations(list(range(n)), 3)
for i in kk:
	mm = min(mm, dp[i[0]][0] + dp[i[1]][1] + dp[i[2]][2])
print(mm)

n, m = list(map(int, input().split()))

char = [m * 100] * n
dig = [m * 100] * n
symb = [m * 100] * n

for i in range(n):
	s = input()
	for j, c in enumerate(s):
		if c in '*#&':
			symb[i] = min(symb[i], j, m - j)
		elif c.isdigit():
			dig[i] = min(dig[i], j, m - j)
		else:
			char[i] = min(char[i], j, m - j)

answer = m * 100
for c in range(n):
	for s in range(n):
		for d in range(n):
			if len(set([c, s, d])) == 3:
				answer = min(answer, char[c] + symb[s] + dig[d])

print(answer)


n, m = map(int, input().split())

w = [input() for _ in range(n)]

inf = 1000000

let = [inf] * n
num = [inf] * n
spec = [inf] * n

def get(i, c):
	l = w[i].find(c)
	r = w[i].rfind(c)
	if l == -1:
		return inf
	return min(l, m - r)

for i in range(n):
	for j in 'qwertyuiopasdfghjklzxcvbnm':
		let[i] = min(let[i], get(i, j))
	for j in '0123456789':
		num[i] = min(num[i], get(i, j))
	for j in '*#&':
		spec[i] = min(spec[i], get(i, j))

ans = inf

for i in range(n):
	for j in range(n):
		if i == j:
			continue
		for k in range(n):
			if i == k or j == k:
				continue
			ans = min(ans, let[i] + num[j] + spec[k])

print(ans)

n, m = list(map(int, input().split()))
l = [input() for _ in range(n)]
ans = n*m*n
dig = [ans for _ in range(n)]
la = [ans for _ in range(n)]
sp = [ans for _ in range(n)]
d = {}
for i in "#*&":
    d[i] = sp
for i in "0123456789":
    d[i] = dig
for i in "abcdefghijklmnopqrstuvwxyz":
    d[i] = la

for i in range(n):
    for j in range(m):
        d[l[i][j]][i] = min(d[l[i][j]][i], min(j, m - j))

for i in range(n):
    for j in range(n):
        for k in range(n):
            if i != k and i != j and j != k:
                ans = min(ans, dig[i]+la[j]+sp[k])
print(ans)


from sys import stdin, stdout

INF = float('inf')
n, m = map(int, stdin.readline().split())
fst = [[INF, INF, INF] for i in range(n)]

for i in range(n):
    s = list(stdin.readline().strip())
    for j in range(m):
        if '9' >= s[j] >= '0' or '9' >= s[-j] >= '0':
            fst[i][0] = min(fst[i][0], j)
        
        if 'z' >= s[j] >= 'a' or 'z' >= s[-j] >= 'a':
            fst[i][1] = min(fst[i][1], j)
        
        if s[j] in '#*&' or s[-j] in '#*&':
            fst[i][2] = min(fst[i][2], j)
        

ans = INF
for i in range(n):
    for j in range(n):
        for z in range(n):
            if i == j or i == z or j == z:
                continue
            
            ans = min(fst[i][0] + fst[j][1] + fst[z][2], ans)

stdout.write(str(ans))

import itertools as it


def special(a):
    return (a == '#' or a == '*' or a == '&')


n, m = list(map(int, input().split()))

INF = 10 ** 6

A = []
for _ in range(n):
    A += [input()]

#print(A)

B = []
for _ in range(3):
    B += [[INF] * n]

#print(B)

for i in range(n):
    if A[i][0].isalpha():
        B[0][i] = 0
    else:
        j = 1
        k = m - 1
        while j <= k:
            #print(j, k)
            if A[i][j].isalpha() or A[i][k].isalpha():
                B[0][i] = j
                break
            j += 1
            k -= 1

for i in range(n):
    if A[i][0].isdigit():
        B[1][i] = 0
    else:
        j = 1
        k = m - 1
        while j <= k:
            if A[i][j].isdigit() or A[i][k].isdigit():
                B[1][i] = j
                break
            j += 1
            k -= 1

for i in range(n):
    if special(A[i][0]):
        B[2][i] = 0
    else:
        j = 1
        k = m - 1
        while j <= k:
            if special(A[i][j]) or special(A[i][k]):
                B[2][i] = j
                break
            j += 1
            k -= 1

result = INF

for pick in it.combinations(list(range(n)), 3):
    #print(pick)
    for perm in it.permutations(list(range(3))):
        #print("perm " + str(perm))
        result = min([result, B[perm[0]][pick[0]] +
                              B[perm[1]][pick[1]] +
                              B[perm[2]][pick[2]]])

print(result)


n, m = map(int, input().split())
a = [0] * n
for i in range(n):
    a[i] = list(input())
g1 = '*#&'
g2 = 'qwertyuiopasdfghjklzxcvbnm'
g3 = '1234567890'

b = [[-1] * 3 for i in range(n)]

for i in range(n):
    f1, f2, f3 = 0, 0, 0
    for j in range(m):
        if a[i][j] in g1 and f1 == 0:
            f1 = 1
            b[i][0] = j
        elif a[i][j] in g2 and f2 == 0:
            f2 = 1
            b[i][1] = j
        elif a[i][j] in g3 and f3 == 0:
            f3 = 1
            b[i][2] = j
    
    f1, f2, f3 = 0, 0, 0
    for j in range(-1, -1 * m, -1):
        if a[i][j] in g1 and f1 == 0:
            f1 = 1
            b[i][0] = min(b[i][0], -1 * j )
        elif a[i][j] in g2 and f2 == 0:
            f2 = 1
            b[i][1] = min(b[i][1], -1 * j )
        elif a[i][j] in g3 and f3 == 0:
            f3 = 1
            b[i][2] = min(b[i][2], -1 * j)

ans = int(1e9)
for i in range(n):
    for j in range(n):
        for k in range(n):
            if i == j or j == k or i == k:
                continue
            #print(i, j, k)
            for l in range(3):
                for g in range(3):
                    for q in range(3):
                        if q != l and l != g and q != g and b[i][l] + b[j][g] + b[k][q] < ans and b[i][l] != -1 and b[j][g] != -1 and b[k][q] != -1:
                            ans = b[i][l] + b[j][g] + b[k][q]
                            #print(ans, i, j, k, l, g, q)
print(ans)

n, m = tuple(map(int, input().split()))
strings = []
for k in range(n):
    strings.append(input())

def f(x):
    if x in ["0","1","2","3","4","5","6","7","8","9"]:
        return "c"
    elif x in ["*","#","&"]:
        return "s"
    else:
        return "l"

for k in range(n):
    strings[k] = list(map(f,strings[k]))
pointers = [1]*n
minsc = []
minss = []
minsl = []

for i in range(n):
    ds,dl,dc = -1,-1,-1
    for j in range(m):
        if strings[i][j] == "c" and (dc == -1 or min((-j)%m,j%m) < dc):
            dc = min((-j)%m,j%m)
        if strings[i][j] == "s" and (ds == -1 or min((-j)%m,j%m)< ds):
            ds = min((-j)%m,j%m)
        if strings[i][j] == "l" and (dl == -1 or min((-j)%m,j%m) < dl):
            dl = min((-j)%m,j%m)
    minsc.append(dc)
    minss.append(ds)
    minsl.append(dl)

m = -1
for i in range(n):
    for j in range(n):
        for k in range(n):
            if i!= j and j!=k and i!=k and not -1 in [minsc[i],minss[j],minsl[k]] and (minsc[i]+minss[j]+minsl[k] < m or m== -1):
                m = minsc[i]+minss[j]+minsl[k]

print(m)


from sys import stdin, stdout
from functools import partial

ONLINE_JUDGE = 1
if ONLINE_JUDGE:
    inp = stdin
    out = stdout
else:
    inp = open('input.txt', 'r')
    out = open('output.txt', 'w')
print_f = partial(print, file=out)

INF = 1000000


def find_dist(s: str, check) -> int:
    m = INF
    for i in range(len(s)):
        if check(s[i]):
            m = min(m, i, len(s) - i)
    return m

n, m = [int(i) for i in inp.readline().split()]

strs = [line.strip() for line in inp.readlines()]

min1 = [find_dist(s, lambda c: ord('0') <= ord(c) <= ord('9')) for s in strs]
min2 = [find_dist(s, lambda c: ord('a') <= ord(c) <= ord('z')) for s in strs]
min3 = [find_dist(s, lambda c: c == '#' or c == '*' or c == '&') for s in strs]

mm = INF
for i1, m1 in enumerate(min1):
    for i2, m2 in enumerate(min2):
        for i3, m3 in enumerate(min3):
            if len({i1, i2, i3}) == 3:
                mm = min(mm, INF, m1 + m2 + m3)

print_f(mm)


from itertools import permutations as perm
n, m = map(int, input().split())
alpha = set([chr(x) for x in range(ord('a'), ord('z') + 1)])
digits = set([str(x) for x in range(0, 10)])
symbol = set(["*", "#", "&"])

d = [[0] * 3 for i in range(n)]
for i in range(n):
    s = input()
    a = b = c = 10**9
    for j in range(len(s)):
        if s[j] in alpha:
            a = min(a, j - 0, m - j)
        elif s[j] in digits:
            b = min(b, j - 0, m - j)
        elif s[j] in symbol:
            c = min(c, j - 0, m - j)
    d[i] = [a, b, c]
    

p = list(perm([x for x in range(n)], 3))
ans = 10**10
for x in p:
    a, b, c = list(x)
    if (a != b and b != c and c != a):
        ans = min(ans, d[a][0] + d[b][1] + d[c][2])
print(ans)

n,m = list(map(int, input().split(" ")))
strings = []
for i in range(n): 
	strings.append(input())

spec = set('#*&')
dig = set('0123456789')
alpha = set('abcdefghijklmnopqrstuvwxyz')


def num(checkset, checkstring):
	m = len(checkstring)+1
	for i, c in enumerate(checkstring):
		if c in checkset:
			m = min(m, i)
			m = min(m, len(checkstring)-i)
	if m < len(checkstring)+1:
		return m
	return -1 
	
ret = float('inf')
	
for i in range(n):
	for j in range(n):
		for k in range(n):
			if i==j or i==k or j==k:
				continue
			else:
				s1 = num(spec, strings[i])
				s2 = num(dig, strings[j])
				s3 = num(alpha, strings[k]) 
				if s1 >= 0 and s2 >= 0 and s3 >= 0:
					ret = min(ret, s1+s2+s3)

print(str(int(ret)))

					 
				


import math, sys, itertools

alpha = list('abcdefghijklmnopqrstuvwxyz')
digit = list('1234567890')
spec = ['*', '&', '#']

def find(s):
	dpos = 10000
	apos = 10000
	spos = 10000
	m = len(s)
	for i in range(len(s)):
		if s[i] in alpha:
			if apos>min(m-i,i):
				apos = min(m-i,i)
		if s[i] in digit:
			if dpos>min(m-i,i):
				dpos = min(m-i,i)
		if s[i] in spec:
			if spos>min(m-i,i):
				spos = min(m-i,i)
	return apos, dpos, spos
		

def main():
	n,m = list(map(int, input().split()))
	st = []
	for i in range(n):
		st.append(input())
	
	al = []
	dig = []
	spec = []
	for i in range(n):
		a, d, s = (find(st[i]))
		al.append(a)
		dig.append(d)
		spec.append(s)
		
	sumn = 10000
	for a in range(n):
		for d in range(n):
			for s in range(n):
				if a!=d!=s:
					if sumn>al[a]+dig[d]+spec[s]:
						sumn = al[a]+dig[d]+spec[s]
	print(sumn)

def __starting_point():
	main()

__starting_point()

n, m = map(int, input().split())
a = ['*', '&', '#']
ans0 = [(10**9, -1)] * n
ans1 = [(10**9, -1)] * n
ans2 = [(10**9, -1)] * n
for i in range(n):
    s = input()
    for j in range(m):
        if s[j] in a:
            ans0[i] = min(ans0[i], (j, i), (m-j, i))
        elif '0' <= s[j] <= '9':
            ans1[i] = min(ans1[i], (j, i), (m-j, i))
        else:
            ans2[i] = min(ans2[i], (j, i), (m-j, i))
ans0.sort()
ans1.sort()
ans2.sort()
ans = 10**5

for i in range(3):
    for j in range(3):
        for k in range(3):
            if ans0[i][1] != ans1[j][1] and ans0[i][1] != ans2[k][1] and ans1[j][1] != ans2[k][1]:
                ans = min(ans, ans0[i][0] + ans1[j][0] + ans2[k][0])
print(ans)

import math

n, m = list(map(int, input().split()))
SYMBOLS = ['&', '*', '#']

strs = []

for i in range(n):
	strs.append(list(input()))

minA = [m] * n
minD = [m] * n
minS = [m] * n
for i, s in enumerate(strs):
	for j, c in enumerate(list(s)):
		dist = min(j, m - j)
		# print(dist)
		if c.isalpha() and minA[i] > dist:
			minA[i] = dist
		if c.isdigit() and minD[i] > dist:
			minD[i] = dist
		if c in SYMBOLS and minS[i] > dist:
			minS[i] = dist

minDist = math.inf
for i in range(n):
	for j in range(n):
		for k in range(n):
			summa = minA[i] + minD[j] + minS[k]
			if i != j and j != k and i != k and minDist > summa:
				minDist = summa

print(minDist)


import string


def get_min_distance(str, alphabet):
    m = len(str)
    mi = m
    for i in range(m):
        if str[i] in alphabet:
            mi = min(i, mi)
            break
    for i in range(m):
        if str[-i] in alphabet:
            mi = min(i, mi)
            break
    return mi

n, m = map(int, input().split())
strs = []
for i in range(n):
    strs.append(input())
mins = []
for i in range(n):
    mins.append([get_min_distance(strs[i], string.digits),
                 get_min_distance(strs[i], string.ascii_lowercase),
                 get_min_distance(strs[i], "#*&")])
# print(mins)

mincost = 10**6
for i in range(n):
    for j in range(n):
        if i == j:
            continue
        for k in range(n):
            if j == k or i == k:
                continue
            mincost = min(mincost, mins[i][0] + mins[j][1] + mins[k][2])
print(mincost)


import sys, itertools

n, m = [int(x) for x in sys.stdin.readline().split()]
lines = [sys.stdin.readline() for _ in range(n)]

classes = [
    [str(x) for x in range(10)],
    [chr(x) for x in range(ord('a'), ord('z')+1)],
    ['#', '*', '&']
]


def steps_to(clazz, line):
    ltr = list(line[:-1])
    rtl = ltr[0:1] + ltr[::-1]
    for i in range(len(ltr)):
        if ltr[i] in clazz or rtl[i] in clazz:
            return i
    return 999999


dists = [
    [steps_to(clazz, line) for clazz in classes]
    for line in lines
]

res = 999999

for perm in itertools.permutations(list(range(3))):

    pick1 = min([(dists[i][perm[0]], i) for i in range(len(dists))])
    pick2 = min([(dists[i][perm[1]], i) for i in range(len(dists)) if i != pick1[1]])
    pick3 = min([(dists[i][perm[2]], i) for i in range(len(dists)) if i != pick1[1] and i != pick2[1]])

    res = min(res, pick1[0]+pick2[0]+pick3[0])

print(res)

"""
50 50
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
[[[[[[1[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[
[[[[[[[[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yz'
sym = '#*&'
val = [[] for _ in range(3)]
val[0] = digit
val[1] = letter
val[2] = sym

#print(val)
n, m = ri()
minm = [[10000000000 for _ in range(3)] for  __ in range(n)]
#print(minm)
for ln in range(n):
    line = input()
    #print(line)
    for i in range(3):
        for mov in range(m):
            if (line[mov] in val[i]) or line[-mov] in val[i]:
                minm[ln][i] = mov
                break

ans = 10**10
#print(minm)
for i1 in range(n):
    for i2 in range(n):
        for i3 in range(n):
            if i1 != i2 and i1 != i3 and i2 != i3:
                ans = min(ans, minm[i1][0] + minm[i2][1] + minm[i3][2])

print(ans)


import math
n,m=list(map(int,input().split()))
#s=input().strip()
l=[]
for i in range(n):
	l.append(input().strip())
digit=0
alpha=0
spec=['&','#','*']
dis=[]
for i in range(n):
	s=l[i]
	j=0
	mindigit=m
	minalpha=m
	minsp=m
	maxdigit=0
	maxalpha=0
	maxsp=0
	while(j<m):
		if(s[j] in spec):
			if(minsp>j):
				minsp=j
			if(maxsp<=j):
				maxsp=j+1
		elif(ord(s[j])>=48 and ord(s[j])<58):
			if(mindigit>j):
				mindigit=j
			if(maxdigit<=j):
				maxdigit=j+1
		elif(ord(s[j])>=97 and ord(s[j])<123):
			if(minalpha>j):
				minalpha=j
			if(maxalpha<=j):
				maxalpha=j+1
		j+=1
	dis.append([min(mindigit,m+1-maxdigit),min(minalpha,m+1-maxalpha),min(minsp,m+1-maxsp)])
mindis=1000
#print(dis)
for  i in range(n):
	for j in range(n):
		for k in range(n):
			if(i!=j and j!=k and k!=i):
				if(dis[i][0]!=m and dis[j][1]!=m and dis[k][2]!=m):
					if(mindis>dis[i][0]+dis[j][1]+dis[k][2]):
						mindis=dis[i][0]+dis[j][1]+dis[k][2]
print(mindis)



def lol():
    ans = INF
    for i in range(n):
        for j in range(n):
            for k in range(n):
                if i == j or j == k or k == i:
                    continue
                
                a, b, c = INF, INF, INF
                for i1 in range(m):
                    if 48 <= ord(s[i][i1]) <= 57 or 48 <= ord(s[i][-i1]) <= 57:
                        a = min(a, i1)
                
                    if 97 <= ord(s[j][i1]) <= 122 or 97 <= ord(s[j][-i1]) <= 122:
                        b = min(b, i1)             
                
                    if s[k][i1] in sim or s[k][-i1] in sim:
                        c = min(c, i1) 
                        
                    if a < INF and b < INF and c < INF:
                        break
                    
                ans = min(ans, a + b + c) 
    return ans


n, m = map(int, input().split())

INF = 10 ** 9
s = [0] * n
sim = ['*', '&', '#']
for i in range(n):
    new = input()
    s[i] = new + new

ans = lol()
            
if ans >= INF:
    print(-1)
else:
    print(ans)

n, m = map(int, input().split())
s = [input() for i in range(0, n)]
dp = [[n*m + 1] * 8 for i in range(0, n + 1)]


def minLenA(s):
    for i in range(0, len(s)):
        if '0' <= s[i] <= '9':
            return i;
    return m + 1

def minLenB(s):
    for i in range(0, len(s)):
        if 'a' <= s[i] <= 'z':
            return i;
    return m + 1

def minLenC(s):
    for i in range(0, len(s)):
        if s[i] in ["#", "*", "&"]:
            return i;
    return m + 1

dp[0][0] = 0
dp[0][1:] = [n*m + 1] * 7

l = [[0] * 3 for i in range(0, n)]
for i in range(0, n):
    l[i][0] = min(minLenA(s[i]),1 + minLenA(list(reversed(s[i]))))
    l[i][1] = min(minLenB(s[i]), 1 +minLenB(list(reversed(s[i]))))
    l[i][2] = min(minLenC(s[i]), 1 + minLenC(list(reversed(s[i]))))
for i in range(1, n + 1):
    for j in range(0, 8):
        dp[i][j] = min(dp[i - 1][j ^ 1] + l[i-1][0], dp[i-1][j],dp[i][j]);
        dp[i][j] = min(dp[i - 1][j ^ 2] + l[i-1][1], dp[i-1][j],dp[i][j]);
        dp[i][j] = min(dp[i - 1][j ^ 4] + l[i-1][2],dp[i-1][j],dp[i][j]);
print(dp[-1][7])

import sys
from itertools import permutations
from operator import itemgetter

def debug(x, table):
    for name, val in table.items():
        if x is val:
            print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
            return None

INF = 1000

def solve():
    n, m = map(int, input().split())
    str_l = []
    for i in range(n):
        moji = []
        line = input()
        for c in line:
            if c == '*' or c == '#' or c == '&':
                moji.append('*')
            elif ord('0') <= ord(c) <= ord('9'):
                moji.append('1')
            else:
                moji.append('a')
        str_l.append(moji)

    # debug(str_l, locals())
    row_nums = []

    for i in range(n):
        kyori = [get_kyori(str_l[i], '1'),
                 get_kyori(str_l[i], 'a'),
                 get_kyori(str_l[i], '*')]

        row_nums.append(kyori)

    ans = INF
    debug(row_nums, locals())

    for i, j, k in permutations((0,1,2)):
        tmp = 0
        kyori_c = row_nums.copy()
        kyori_c.sort(key=itemgetter(i))
        tmp += kyori_c[0][i]
        kyori_c = kyori_c[1:]
        kyori_c.sort(key=itemgetter(j))
        tmp += kyori_c[0][j]
        kyori_c = kyori_c[1:]
        kyori_c.sort(key=itemgetter(k))
        tmp += kyori_c[0][k]
        ans = min(tmp, ans)

    print(ans)

def get_kyori(str1, c):
    res = INF

    if c in str1:
        i1 = str1.index(c)
        if i1 > len(str1) // 2:
            i1 = len(str1) - i1
        i2 = len(str1) - str1[::-1].index(c)
        if i2 > len(str1) // 2:
            i2 = len(str1) - i2 + 1
        res = min(i1, i2)

    return res

def __starting_point():
    solve()
__starting_point()

symb = ["#", "*", "&"]
inf = float("inf")
def isAlpha(char):
    return 97 <= ord(char) <= 122

def first(s):
    l = len(s)
    alpha = inf
    d =  inf
    sym = inf
    for i in range(l):
        c = s[i]
        if isAlpha(c):
            alpha = min(alpha, i)
        if c.isdigit():
            d = min(d, i)
        if c in symb:
            sym = min(sym, i)
            
    for i in range(1, l + 1):
        c = s[l - i]
        if isAlpha(c):
            alpha = min(alpha, i)
        if c.isdigit():
            d = min(d, i)
        if c in symb:
            sym = min(sym, i)
    
    return [alpha, d, sym]

n, m = [int(item) for item in input().split()]
ans = float("inf")
s = []
for i in range(n):
    s.append(input())

firsts = []
for i in range(n):
    firsts.append(first(s[i]) + [i])

a = sorted(firsts, key = lambda x : x[0])
b = sorted(firsts, key = lambda x : x[1])
c = sorted(firsts, key = lambda x : x[2])

for i in range(min(m, 3)):
    for j in range(min(m, 3)):
        for k in range(min(m, 3)):
            #print(a[i][0] + a[j][1] + a[k][2], a[i][3], a[j][3], a[k][3])
            if a[i][3] != b[j][3] and a[i][3] != c[k][3] and b[j][3] != c[k][3]:
                ans = min(ans, a[i][0] + b[j][1] + c[k][2])

#print(a)
#print(b)
#print(c)

print(ans)

read = lambda: map(int, input().split())
n, m = read()
a = [input() for i in range(n)]
c1 = '1234567890'
c2 = 'qwertyuiopasdfghjklzxcvbnm'
c3 = '#&*'
inf = 10 ** 20
l1 = [inf] * n
l2 = [inf] * n
l3 = [inf] * n
for i in range(n):
    for j in range(m):
        if a[i][j] in c1: l1[i] = min(l1[i], j, m - j)
        if a[i][j] in c2: l2[i] = min(l2[i], j, m - j)
        if a[i][j] in c3: l3[i] = min(l3[i], j, m - j)
ans = inf
for i in range(n):
    for j in range(n):
        for k in range(n):
            if len({i, j, k}) < 3: continue
            cur = l1[i] + l2[j] + l3[k]
            ans = min(ans, cur)
print(ans)