#abc320c. C - Slot Strategy 2 (Easy)

C - Slot Strategy 2 (Easy)

Score : 300300 points

问题描述

本题是问题 G 的简化版本。

存在一台拥有三个转轮的老虎机。
ii 个转轮上的符号排列由字符串 SiS_i 表示。这里,SiS_i 是一个长度为 MM 的由数字组成的字符串。

每个转轮都有一个对应的按钮。对于每一个非负整数 tt,在转轮开始旋转后恰好 tt 秒时,高桥可以选择按下任意一个按钮或什么也不做。
如果他在转轮开始旋转后恰好 tt 秒时按下第 ii 个转轮对应的按钮,那么第 ii 个转轮将会停止,并显示出 SiS_i 中的第 ((tmodM)+1)((t \mod M)+1) 个字符。
这里,tmodMt \mod M 表示 tt 除以 MM 后的余数。

高桥希望让所有转轮停止显示相同的字符。
找出从转轮开始旋转到所有转轮都停止所需的最小可能秒数,以实现他的目标。
如果这是不可能的,请报告这一情况。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

This problem is an easier version of Problem G.

There is a slot machine with three reels.
The arrangement of symbols on the ii-th reel is represented by the string SiS_i. Here, SiS_i is a string of length MM consisting of digits.

Each reel has a corresponding button. For each non-negative integer tt, Takahashi can either choose and press one button or do nothing exactly tt seconds after the reels start spinning.
If he presses the button corresponding to the ii-th reel exactly tt seconds after the reels start spinning, the ii-th reel will stop and display the ((tmodM)+1)((t \bmod M)+1)-th character of SiS_i.
Here, tmodMt \bmod M denotes the remainder when tt is divided by MM.

Takahashi wants to stop all the reels so that all the displayed characters are the same.
Find the minimum possible number of seconds from the start of the spin until all the reels are stopped so that his goal is achieved.
If this is impossible, report that fact.

Constraints

  • 1M1001 \leq M \leq 100
  • MM is an integer.
  • SiS_i is a string of length MM consisting of digits.

Input

The input is given from Standard Input in the following format:

MM

S1S_1

S2S_2

S3S_3

Output

If it is impossible to stop all the reels so that all the displayed characters are the same, print -1.
Otherwise, print the minimum possible number of seconds from the start of the spin until such a state is achieved.

Sample Input 1

10
1937458062
8124690357
2385760149

Sample Output 1

6

Takahashi can stop each reel as follows so that 66 seconds after the reels start spinning, all the reels display 8.

  • Press the button corresponding to the second reel 00 seconds after the reels start spinning. The second reel stops and displays 8, the ((0mod10)+1=1)((0 \bmod 10)+1=1)-st character of S2S_2.
  • Press the button corresponding to the third reel 22 seconds after the reels start spinning. The third reel stops and displays 8, the ((2mod10)+1=3)((2 \bmod 10)+1=3)-rd character of S3S_3.
  • Press the button corresponding to the first reel 66 seconds after the reels start spinning. The first reel stops and displays 8, the ((6mod10)+1=7)((6 \bmod 10)+1=7)-th character of S1S_1.

There is no way to make the reels display the same character in 55 or fewer seconds, so print 66.

Sample Input 2

20
01234567890123456789
01234567890123456789
01234567890123456789

Sample Output 2

20

Note that he must stop all the reels and make them display the same character.

Sample Input 3

5
11111
22222
33333

Sample Output 3

-1

It is impossible to stop the reels so that all the displayed characters are the same.
In this case, print -1.

update @ 2024/3/10 01:38:30