#-*-coding:utf-8 -*-
大佬方法好强 改成了python版本
n = int(raw_input())
p = map(int ,raw_input().strip().split()[:n])
k = int(raw_input())
sum1 = [0 for i in range(n+1)]
ans = [-1 for i in range(n+1)]#存储第一次出现某一个数的下标的值ans【1】 = 2就是表示余数为1在是第二个数
res = -1
for i in range(1,n+1):
sum1[i] =sum1[i-1]+p[i-1]
m = sum1[i]%k
if ans[m]==-1:
ans[m]=i
else:
res = max(res,i-ans[m])
print max(0,res)