n = int(input()) K = list(map(int, input().split())) A = [[] for _ in range(n)] for i in range(n): input() A[i] = list(map(int, input().split())) def ok(x): C = [] all_ = [] for row in A: c = sum(a >= x for a in row) if c == len(row): all_.append(c) else: C.append(c) if not C: return True C.sort() all_.sort(reverse=True) for k in K[::-1]: if C and C[-1] >= k: return True elif all_ and all_[0] >= k: return True if C[-1] == k - 1: if all_ and all_[-1] < k - 1: all_.pop() else: C.pop() elif all_: all_.pop() else: return False if not C and all_: return True return False l = 1 r = 1 << 30 while r - l > 1: mid = (l + r) // 2 if ok(mid): l = mid else: r = mid print(l)