import
java.util.Arrays;
class
GFG
{
static
int
minCost(String str,
int
K)
{
int
n = str.length();
int
res =
999999999
, count =
0
, a, b;
int
cnt[] =
new
int
[
27
];
Arrays.fill(cnt,
0
);
for
(
int
i =
0
; i < n; i++)
cnt[str.charAt(i) -
'a'
+
1
]++;
for
(
int
i =
1
; i < (
26
- K +
1
); i++)
{
a = i;
b = i + K;
count =
0
;
for
(
int
j =
1
; j <=
26
; j++)
{
if
(cnt[j] >
0
)
{
if
(j >= a && j >= b)
count = count + (Math.min(j - b,
25
- j + a +
1
)) * cnt[j];
else
if
(j <= a && j <= b)
count = count + (Math.min(a - j,
25
+ j - b +
1
)) * cnt[j];
}
}
res = Math.min(res, count);
}
for
(
int
i =
26
- K +
1
; i <=
26
; i++)
{
a = i;
b = (i + K) %
26
;
count =
0
;
for
(
int
j =
1
; j <=
26
; j++)
{
if
(cnt[j] >
0
)
{
if
(j >= b && j <= a)
count = count + (Math.min(j - b, a - j)) * cnt[j];
}
}
res = Math.min(res, count);
}
return
res;
}
public
static
void
main(String[] args)
{
String str =
"abcdefghi"
;
int
K =
2
;
System.out.println(minCost(str, K));
}
}