local mmi, mma = math.min, math.max local n, _k = io.read("*n", "*n") local t = {} local a = {} for i = 1, n do local ai, m = io.read("*n", "*n") a[i] = ai local ret = 0 for j = 1, m do local b = io.read("*n") ret = mma(ret, t[b] + ai - a[b]) end if 1 < i then ret = mma(ret, t[i - 1]) end t[i] = ret end local ans = 0 for i = 1, n do ans = mma(ans, t[i]) end print(ans)