import
java.util.HashMap;
import
java.util.HashSet;
class
GFG {
static
long
p =
1299827
;
static
long
mod = (
long
) 1E11 +
7
;
static
long
M;
static
boolean
check(
int
[][] array,
int
len) {
if
(len ==
0
)
return
true
;
HashMap<Long, Integer> freq =
new
HashMap<Long, Integer>();
for
(
int
i =
0
; i < M; i++) {
long
curr_hash =
0
, pow =
1
;
HashSet<Long> found_hashes =
new
HashSet<Long>();
for
(
int
j =
0
; j < len; j++) {
curr_hash = (curr_hash * p) % mod;
curr_hash += array[i][j];
if
(j != len -
1
)
pow = (pow * p) % mod;
}
found_hashes.add(curr_hash);
for
(
int
j = len; j < array[i].length; j++) {
curr_hash += mod;
curr_hash -= (array[i][j - len] * pow) % mod;
curr_hash %= mod;
curr_hash = curr_hash * p;
curr_hash %= mod;
curr_hash += array[i][j];
curr_hash %= mod;
found_hashes.add(curr_hash);
}
while
(found_hashes.size() >
0
) {
long
h = found_hashes.iterator().next();
found_hashes.remove(h);
if
(freq.containsKey(h)) {
freq.put(h, freq.get(h) +
1
);
}
else
{
freq.put(h,
1
);
}
if
(freq.get(h) == M)
return
true
;
}
}
return
false
;
}
public
static
int
longestCommonSubpath(
long
N,
int
[][] array) {
M = N;
int
minlen = Integer.MAX_VALUE;
for
(
int
i =
0
; i < array.length; i++) {
minlen = Math.min(minlen, (
int
) array[i].length);
}
int
start =
0
, end = minlen, mid =
0
;
while
(start <= end) {
mid = (start + end) /
2
;
if
(check(array, mid)) {
start = mid +
1
;
}
else
{
end = mid -
1
;
}
}
return
end;
}
public
static
void
main(String args[]) {
int
[][] arr = { {
0
,
1
,
2
,
3
,
4
}, {
2
,
3
,
4
}, {
4
,
0
,
1
,
2
,
3
} };
long
N = arr.length;
System.out.println(longestCommonSubpath(N, arr));
}
}