You are given *n* *k*-digit integers. You have to rearrange the digits in the integers so that the difference between the largest and the smallest number was minimum. Digits should be rearranged by the same rule in all integers.
The first line contains integers *n* and *k* — the number and digit capacity of numbers correspondingly (1 ≤ *n*, *k* ≤ 8). Next *n* lines contain *k*-digit positive integers. Leading zeroes are allowed both in the initial integers and the integers resulting from the rearranging of digits.
6 4
5237
2753
7523
5723
5327
2537
2700
3 3
010
909
012
3
7 5
50808
36603
37198
44911
29994
42543
50156
20522
5 5
61374
74304
41924
46010
09118
64592
8 8
68785928
11981277
32480720
72495162
69969623
42118868
64235849
81412116
52901157
7 1
1
0
8
5
4
9
8
9
3 8
34848224
16307102
25181102
8612277
2 8
13633861
68468345
14445725
4 4
0950
0634
9264
8684
3738
6 5
65777
80932
32260
49089
00936
85557
41439
5 6
687443
279213
765651
611680
500192
258067
8 6
034753
917195
222679
778596
980006
467267
482763
807481
647026
8 6
075967
240855
352399
791547
103244
982259
409866
926586
491255
3 1
7
2
9
7
6 4
5407
4617
3050
7647
8647
1993
6474
8 5
47553
55138
81768
78902
50691
73010
93969
01675
71123
8 7
5945843
9094433
0750024
6255984
1784849
7275947
6513944
0145523
5152379
8 7
8112819
8982110
5457941
4575033
5203331
7410823
0532182
8151054
6194602
8 8
63315032
20587190
05461152
76872565
71177578
53541174
00451913
85740357
60622457
2 3
135
725
4
7 1
9
5
8
9
7
6
9
4
5 3
560
978
543
846
714
435
7 2
53
74
84
62
14
77
59
69
3 4
0537
2174
5299
3583
7 5
13532
16394
97663
73133
22712
58185
65035
26455
8 5
07936
07927
46068
99158
90958
41283
59266
87841
52364
8 6
867468
695388
700723
444270
545657
178053
315040
554471
559559
7 7
6575460
6965366
1912357
7080608
2561692
5209630
0439095
5917123
1 2
96
0
1 3
289
0
1 8
78795220
0
8 7
2407792
7023368
2609925
0587109
3543873
6602371
4579875
9893509
6790457
4 6
065169
150326
924608
490012
488134
4 4
8851
6190
0521
1659
6596
4 4
4381
3147
7017
5593
3690
8 4
0344
9196
1379
5470
0989
8316
7096
7918
7801
1 6
430254
0
8 1
4
0
8
5
9
0
4
7
9
5 2
60
08
77
66
03
74
3 1
9
8
2
7
7 2
89
00
59
90
99
22
55
99
2 4
7694
6577
712
8 8
68785928
11981277
32480720
72495162
69969623
42118868
64235849
81412116
52901157
2 7
9183508
9276377
26912
5 4
7411
3080
9578
5902
3225
6498
3 4
0136
4556
4268
2134
6 8
99358096
38390629
71597322
35940809
48949759
66204248
53570178
7 2
23
11
88
25
22
45
10
78
2 3
834
630
24
4 2
87
03
95
23
48
2 8
10715643
97664296
1244714
6 1
9
3
1
3
4
5
8
8 5
47553
55138
81768
78902
50691
73010
93969
01675
71123
4 4
7603
0859
5241
7680
5518
1 7
4605461
0
3 4
3061
3404
6670
2916
8 4
1847
0962
3216
0772
6399
3082
7997
0625
7246
2 6
834527
764560
577
5 6
959808
303464
414335
758650
828038
486245
4 1
0
7
5
1
7
6 7
4565736
9842969
1412800
6411011
5744909
3791659
4066781
4 1
0
7
5
1
7
1 3
250
0
2 1
2
0
2
8 8
96805230
73119021
06552907
86283347
88650846
19155689
37032451
19310120
53604668
3 2
64
94
65
10
8 4
8008
4983
0295
0353
5838
1960
0270
7144
7475
4 8
22025344
54085308
77633421
59238322
7681556
5 3
504
878
599
683
083
615
5 4
7663
4755
2941
4588
0232
5346
6 2
97
57
40
99
22
94
77
6 7
4104025
1370353
3472874
5258456
5595923
0279404
2790148
8 2
42
86
25
30
27
64
67
38
61
5 2
52
22
05
37
74
51
2 2
63
50
13
6 7
4104025
1370353
3472874
5258456
5595923
0279404
2790148
6 2
95
56
06
46
77
51
62
3 5
97424
96460
47766
9536
2 3
596
246
35
3 1
1
2
2
1
4 2
87
03
95
23
48
7 5
41078
41257
35324
70082
66783
99954
85784
56901
8 7
8943041
2427704
3775080
2956111
1345704
0937172
1979973
7081540
3544246
6 6
505845
903151
055779
733849
508266
029177
249045
4 4
1871
9417
7444
4294
5368
2 5
60106
07866
5224
3 3
195
860
567
258
8 5
68186
57779
78079
47451
69788
82172
75373
50157
32237
4 7
5342341
5194611
4032103
8739798
4056779
4 8
91401735
53979237
20857777
94594293
34567247
1 2
95
0
6 4
0443
7108
7211
4287
6439
7711
5301
6 7
5794383
4078451
0263676
7682294
7436158
3363189
3560125
2 5
07259
51985
23657
3 3
624
125
097
247
8 1
9
7
6
2
9
6
4
8
7
6 3
530
862
874
932
972
157
442
3 2
51
39
97
58
8 4
4650
1735
4269
8023
0948
9685
3675
6017
6836
5 3
168
513
110
386
501
403
6 2
01
81
60
27
23
67
70
4 4
2759
7250
3572
8067
2028
8 5
12658
00588
23491
09985
63973
78517
98187
29863
68592
3 1
5
4
2
3
7 8
24925537
07626274
77060131
82415056
70422753
60455207
32176884
54680138
5 8
94157433
85577189
62547277
11815893
35445851
15679126
5 5
31164
27213
17981
48806
01273
33367
3 6
743197
172242
635654
261245
4 6
760130
653002
902824
380915
268111
8 8
83239439
62184887
58968944
39808261
68740623
38480328
81965504
52600488
44481119
8 2
99
20
22
39
33
60
54
08
91
1 7
3545113
0
6 7
3761949
8095136
4875085
5017784
4459097
4354762
4126934
6 8
50157346
63836375
03176371
83637145
28631038
18617159
24702445
1 5
84932
0
4 3
204
515
280
840
467
8 2
40
41
02
55
26
52
60
25
58
2 5
90526
32565
586
4 4
3058
2370
0288
5983
2972
6 7
9085507
7716507
1952887
6569746
1900754
9212439
3180457
5 2
01
07
63
71
99
89
6 4
4505
3672
4248
2783
9780
6579
4484
2 3
281
498
127
8 5
16966
36762
49579
71703
66646
41125
94022
26623
66868
1 6
170086
0
4 1
4
2
2
2
2
3 8
12418144
74773130
10504811
22901234
6 7
3761949
8095136
4875085
5017784
4459097
4354762
4126934
from itertools import permutations
n,k = map(int,input().split())
b = []
for i in range(1,k+1):
    b.append(i)
a = [i for i in permutations(b)]
lst = []
ans = float('inf')
for i in range(n):
    lst.append(input())
def per(perm,lst):
    global n,k,ans
    for i in perm:
        mn = float('inf')
        mx = 0
        for nm in lst:
            s = ''
            for b in i:
                s+=nm[b-1]
            s = int(s)
            mn = min(mn,s)
            mx = max(mx,s)
        ans = min(ans,mx-mn)
    return ans
print(per(a,lst))