Problem:
クイズ
あなたはクイズ番組のディレクターである．
クイズ番組には解答者として
N
人が出演予定であり，それぞれ 1 から
N
まで番号付けられている．
問題は
M+1
問出題する予定であり，それぞれの問題は 1 から
M+1
まで番号付けられている．
問題は番号順に出題され，それぞれ早押しで最初に正解した人にのみ得点が入る．
i
問目の問題の得点は整数
S
i
である．
M+1
問目の問題を終えた時点で総合得点が最大の人が優勝する．
ただし，最大得点の人が複数人存在する場合，優勝者は存在しない．
現在
M
問目まで配点を決めたので，
M+1
問目の配点を決めようと考えている． 
最後の問題は，誰でも逆転できる点数にするのがクイズ番組のお約束である． 
しかし，その場で解答者たちの総合得点を見て問題の点数を決めると，解答者たちのやる気を削ぐ可能性がある．そこで， どんな点数状況でも全員に逆転のチャンスがあるような点数設定をあらかじめ考えることにした．
幸い，1 から
M
問目まではそれぞれ正解する可能性がある解答者が分かっている．
M+1
問目は全員が正解する可能性のある問題である．正解する可能性がある解答者の中で，早押しで正解した1名のみが問題の得点
S
i
を得る．問題への解答は正解した解答者が現れた時点で締め切られ，同じ解答者は何度でも解答を行うことができるため，ある問題の得点
S
i
を誰も得られない場合は考慮しなくてよい．また，複数人の解答者がある問題の得点
S
i
を重複して獲得したり，得点を分け合ったりすることもない．
各問の配点と正解可能な解答者の情報を基に， 起こりうるどのような得点状況においても，最後の問題を正解すれば必ず誰でも優勝できるように最後の問題の点数
S
M+1
を設定したい． 条件を満たす整数
S
M+1
として最小の値を求めよ．
Input
入力データセットは複数のケースから構成される．各ケースは次のような形式である．
N
M
S
1
k
1
c
1,1
...
c
1,k
1
...
S
M
k
M
c
M,1
...
c
M,k
M
1 行目には，解答者の数
N
と最後の問題を除いた問題の個数
M
が与えられる．
続く
M
行には，問題 1 〜
M
についての解答できる可能性のある解答者の情報が与えられる．そのうち
i
行目には，
i
問目の得点
S
i
と，
i
問目に正解する可能性がある解答者の人数
k
i
が与えられ，またその直後に
k
i
個の数
c
i,1
... c
i,k
i
が与えられる．
c
i,j
(
1 ≤ j ≤ k
i
) はそれぞれ ，
i
問目に正解する可能性がある解答者の番号を表す．
入力の終わりは 2 つのゼロからなる行で示す．データセットの個数は最大でも 30 個を超えない．
入力で与えられる数値は全て整数であり，以下の条件を満たす．
2 ≤ N ≤ 10,
000
1 ≤ M ≤ 1,
000
1 ≤ S
i
≤ 100
1 ≤ k
i
≤ N
1 ≤ c
i,1
< ... < c
i,k
i
≤ N
Σk
i
≤  100,000
ただし，出力すべき
S
M+1
はこの範囲を超える場合があることに注意せよ．
Output
各データセットについて条件を満たすような最後の問題の点数
S
M+1
の最小値を 1 行に出力せよ．
Sample Input
3 2
5 2 1 3
8 2 2 3
2 3
8 2 1 2
3 1 1
5 1 2
2 5
100 1 1
100 1 1
100 1 1
100 1 1
100 1 1
3 4
5 1 1
5 1 2
100 2 1 3
100 2 2 3
0 0
Output for Sample Input
14
11
501
196

Submitted solutions:

code_snippet: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }

/*
 <url:>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

int N,M;
ll solve(){
    ll res = 0;
    vector<ll> scores(N,0),minscores(N,0);
    for(int i = 0; i < M;i++){
        int S,k; cin >> S >> k;
        if(k == 1){
            int c; cin >> c;
            c--;
            scores[c] += S;
            minscores[c] += S;
        }else{
            for(int j = 0; j < k;j++){
                int c; cin >> c; c--;
                scores[c] += S;
            }
        }
    }
    
    for(int i = 0; i < N;i++){
        ll T = INF;
        for(int j = 0; j < N;j++){
            if(i == j) continue;
            T = min(T,minscores[j]);
        }
        if(scores[i] < T) continue;
        res = max(res,scores[i] - T + 1);
    }
    return res;
}
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    while(cin >> N >> M,N){
        cout << solve() << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 660
memory_kb: 3596
final_rank: 12

code_snippet: #include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

#define MAX 30
#define MAX_POINT 100
#define MAX_NUM 10000

int main(void){
    /*関数の初期化*/
    int i, j, k;
    int N,M;
    int num,tmp,top;
    int index;

    for(i=0;i<MAX;i++){
        /*入力を取得する*/
        cin >> N >> M;

        if((N==0)&&(M==0)){
            break;
        } else if ((N>MAX_NUM)||(N<2)||(M>1000)||(M<1))
        {
            continue;
        }

        vector<int> decisive(M);
        vector<int> max(N);
        vector<int> min(N);
        vector<int> point(M);
        num=0;

        for(j=0;j<M;j++){
            cin >> point[j] >> num;
            for(k=0;k<num;k++){
                cin >> tmp;
                if(num==1){
                    decisive[j] = tmp;
                }
                max[tmp-1] += point[j];
            }
        }
        
        /*取得可能点数の最大値を求める*/
        auto max_iterator = max_element(max.begin(), max.end());
        index = distance(max.begin(), max_iterator);
        top = *max_iterator;

        /*取得可能点数の最小値を求める*/
        for(j=0;j<decisive.size();j++){
            if(decisive[j] != 0){
                min[decisive[j]-1] = point[j];
            }
        }

        min[index] = top;

        auto min_iterator = min_element(min.begin(), min.end());
        int low = *min_iterator;

        /*最終問題の適当な点数を表示*/
        cout << top - low + 1 << endl; 
    }

    return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 3508
final_rank: 3

code_snippet: #include <stdio.h>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

#define MAX 30
#define MAX_POINT 100
#define MAX_NUM 10000

int main(void){
    int i, j, k;
    int N,M;
    int num,tmp,top;
    int index;

    for(i=0;i<MAX;i++){
        cin >> N >> M;

        if((N==0)&&(M==0)){
            break;
        } else if ((N>MAX_NUM)||(N<2)||(M>1000)||(M<1))
        {
            continue;
        }

        vector<int> decisive(M);
        vector<int> max(N);
        vector<int> min(N);
        vector<int> point(M);
        num=0;

        for(j=0;j<M;j++){
            cin >> point[j] >> num;
            for(k=0;k<num;k++){
                cin >> tmp;
                if(num==1){
                    decisive[j] = tmp;
                }
                max[tmp-1] += point[j];
            }
        }
        
        auto max_iterator = max_element(max.begin(), max.end());
        index = distance(max.begin(), max_iterator);
        top = *max_iterator;

        for(j=0;j<decisive.size();j++){
            if(decisive[j] != 0){
                min[decisive[j]-1] = point[j];
            }
        }

        min[index] = top;

        auto min_iterator = min_element(min.begin(), min.end());
        int low = *min_iterator;

        cout << top - low + 1 << endl; 
    }

    return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 3492
final_rank: 2

code_snippet: #include <bits/stdc++.h>
#include <unordered_map>
#include <stdlib.h>
using namespace std;
#define rep(i, a, n) for(ll i = a; i < n; i++)
#define rrep(i, a, n) for(ll i = a; i >= n; i--)
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(x) (x).begin(), (x).end()
//constexpr ll MOD = 1000000007;
constexpr ll MOD = 998244353;
constexpr int IINF = 1001001001;
constexpr ll INF = 1LL<<60;
template<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}
template<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}


ll gcd(ll a, ll b){
    if(a%b == 0){
      return b;
    }else{
      return gcd(b, a%b);
    }
}

ll lcm(ll a, ll b){
    return a*b / gcd(a, b);
}

ll powMod(ll x, ll n) {
    if (n == 0) return 1 % MOD;
    ll val = powMod(x, n / 2);
    val *= val;
    val %= MOD;
    if (n % 2 == 1) val *= x;
    return val % MOD;
}

int main() {
    while(true){
        ll n, m; cin >> n >> m;
        if(n == 0) break;
        vector<ll> mini(n,0), maxi(n,0);
        rep(i,0,m){
            ll s, k; cin >> s >> k;
            if(k == 1){
                ll c; cin >> c;
                c--;
                mini[c] += s;
                maxi[c] += s;
            }
            else{
                rep(j,0,k){
                    ll c; cin >> c;
                    c--;
                    maxi[c] += s;
                }
            }
        }
        vector<pll> ma(n), mi(n);
        rep(i,0,n){
            ma[i] = {maxi[i],i};
            mi[i] = {mini[i],i};
        }
        sort(all(mi));
        sort(all(ma), greater<pll>());
        ll ans = 1;
        rep(i,0,2)rep(j,0,2){
            auto [a1, b1] = ma[i];
            auto [a2, b2] = mi[j];
            if(b1 == b2) continue;
            chmax(ans, a1-a2+1);
        }
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 3660
final_rank: 6

code_snippet: #include <bits/extc++.h>

using ll = long long;
using namespace std;
#define REP(i, n) for (ll i = 0; i < ll(n); i++)

bool solve() {
  ll n, m;
  cin >> n >> m;
  if (n == 0 and m == 0) return false;
  vector<ll> a(n), b(n);
  ll ans = 0;
  REP(i, m) {
    ll s, k;
    cin >> s >> k;
    REP(j, k) {
      ll x;
      cin >> x;
      x--;
      if (k == 1) a[x] += s;
      b[x] += s;
    }
  }
  REP(i, n) {
    REP(j, n) {
      if (i == j) continue;
      ans = max(ans, b[j] - a[i]);
    }
  }
  cout << ans + 1 << endl;
  return true;
}

int main() {
  while (solve());
}
accuracy: 1
time_ms: 720
memory_kb: 3712
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;

#define rep(i,n) for(ll i=0;i<n;++i)
#define all(a) (a).begin(),(a).end()
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
template<class T> T div_floor(T a, T b) { return a / b - ((a ^ b) < 0 && a % b); }
template<class T> T div_ceil(T a, T b) { return a / b + ((a ^ b) > 0 && a % b); }
template <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; }
template <typename T, typename U> inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; }

template<typename T>
ostream &operator<<(ostream &os, const vector<T> &a){
	if (a.empty()) return os;
	os << a.front();
	for (auto e : a | views::drop(1)){
		os << ' ' << e;
	}
	return os;
}
void dump(auto... vs) {
	((cout << vs << ' '), ...) << endl;
}


void solve(ll N,ll M) {
	vector<ll> U(N,0);
	vector<ll> L(N,0);
	rep(i,M){
		ll S,k;
		cin>>S>>k;
		rep(j,k){
			ll p;
			cin>>p;
			p--;
			if (k==1)L[p]+=S;
			U[p]+=S;
		}
	}
	vector<ll> P(N);
	rep(i,N)P[i]=i;
	sort(all(P),[&](ll i,ll j){
		return U[i]>U[j];
	});
	ll u1=P[0];
	ll u2=P[1];
	sort(all(P),[&](ll i,ll j){
		return L[i]<L[j];
	});
	ll l1=P[0];
	ll l2=P[1];
	ll ans=0;
	for (ll i:{l1,l2}){
		for (ll j:{u1,u2}){
			if (i!=j)chmax(ans,U[j]-L[i]);
		}
	}
	ans++;
	cout<<ans<<endl;
    return;
}


int main() {
    while (true){
		ll N,M;
		cin>>N>>M;
		if (N==0)break;
        solve(N,M);
    }
    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 3840
final_rank: 8

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using a2 = array<ll, 2>;
using a3 = array<ll, 3>;

template <typename A> void chmin(A &l, const A &r) {
    if(r < l)
        l = r;
}
template <typename A> void chmax(A &l, const A &r) {
    if(l < r)
        l = r;
}

ll mod = 998244353;

ll n, m;
void input() { cin >> n >> m; }

void solve() {
    vector<ll> mx(n,0), mn(n,0);
    for(int i = 0; i < m; i++) {
        ll s, k;
        cin >> s >> k;
        for(int j = 0; j < k; j++) {
            ll a;
            cin >> a;
            a--;
            if(k == 1)
             mn[a]+= s;
             mx[a]+=s;
        }
    }
    auto mxitr = max_element(mx.begin(),mx.end());
    auto mnitr = min_element(mn.begin(),mn.end());

    ll ans = 0;
    for(int i=0;i<n;i++)
    {
        if(mxitr-mx.begin()!=i)
        {
            chmax(ans,*mxitr-mn[i]+1);
        }
        if(mnitr-mn.begin()!=i)
        {
            chmax(ans,mx[i]-mnitr[i]+1);
        }
    }

    cout << ans<< endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    while(1) {
        input();
        if(n == 0)
            break;
        solve();
    }

    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3588
final_rank: 5

code_snippet: #include <algorithm>
#include <array>
#include <atomic>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfenv>
#include <cfloat>
#include <chrono>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <condition_variable>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <filesystem>
#include <forward_list>
#include <fstream>
#include <functional>
#include <future>
#include <initializer_list>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <mutex>
#include <new>
#include <numeric>
#include <optional>
#include <ostream>
#include <queue>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <set>
#include <shared_mutex>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <string_view>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <variant>
#include <vector>
using namespace std;

#define rep2(i, m, n) for (int i = (m); i < (n); ++i)
#define rep(i, n) rep2(i, 0, n)
#define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i)
#define drep(i, n) drep2(i, n, 0)
#define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__)
#define INF (long long)1001001001001001001
#define inf 1001001000
#define fi first
#define se second

using ll = long long;
using vi = vector<int>;
using vvi  = vector<vector<int>>;
using vl = vector<long long>;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
using LL = __int128_t;

ll gcd(ll x, ll y) {if (x == 0) return y;	return gcd(y%x, x);} 
ll lcm(ll x, ll y) { __int128_t xx,yy; xx=x; yy=y; __int128_t ans=xx * yy / gcd(x, y); ll ans2=ans; return ans2; }
template<typename T>
T POW(T x, ll n){T ret=1;	while(n>0){		if(n&1) ret=ret*x;		x=x*x;		n>>=1;	}	return ret;}
template<typename T>
T modpow(T a, ll n, T p) {	if(n==0) return (T)1;  if (n == 1) return a % p;  if (n % 2 == 1) return (a * modpow(a, n - 1, p)) % p;  T t = modpow(a, n / 2, p);  return (t * t) % p;}
template<typename T>
T modinv(T a, T m) {	if(m==0)return (T)1;	T b = m, u = 1, v = 0;	while (b) {		T t = a / b;		a -= t * b; swap(a, b);		u -= t * v; swap(u, v);	}	u %= m;	if (u < 0) u += m;	return u;}
ll REM(ll a, ll b){ return (a % b + b) % b;}
ll QUO(ll a, ll b){ return (a - REM(a, b)) / b;}
/*
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dist1(1, 100); [1,100]
int random_value = dist1(gen);
*/
/*
auto start = chrono::steady_clock::now(); //時間計測の開始
auto now = std::chrono::steady_clock::now(); //現在時刻と開始時刻の差を測定
double elapsed = std::chrono::duration<double>(now - start).count(); //時間をdouble型で取得
*/
/*
const int MAXCOMB=510000;
std::vector<mint> FAC(MAXCOMB), FINV(MAXCOMB), INV(MAXCOMB);
void COMinit() {FAC[0] = FAC[1] = 1;FINV[0] = FINV[1] = 1;INV[1] = 1;for (int i = 2; i < MAXCOMB; i++) {FAC[i] = FAC[i - 1] * i;INV[i] = mint(0) - INV[mint::mod() % i] * (mint::mod() / i);FINV[i] = FINV[i - 1] * INV[i];}}
mint COM(int n, int k) {if (n < k) return 0;if (n < 0 || k < 0) return 0;return FAC[n] * FINV[k] * FINV[n - k];}
*/

template <typename T> inline bool chmax(T &a, T b) { return ((a < b) ? (a = b, true) : (false));}
template <typename T> inline bool chmin(T &a, T b) { return ((a > b) ? (a = b, true) : (false));}

/////////////////////////////////////////////////////////////////////////////////////////
int SOLVEFIN = 0;

void solve(){
  int n, m;
  cin >> n >> m;
  if (n == 0) {
    SOLVEFIN=1;
    return;
  }
  vi s(m);
  vi k(m);
  vvi posset(m);
  vi maxscore(n,0);
  vi minscore(n,0);
  vi sortscore;

  rep(i, m) {
    cin >> s[i] >> k[i];
    rep(j, k[i]) {
      int v;
      cin>>v;
      v--;
      if (k[i] == 1) {
        minscore[v]+=s[i];
      }
      maxscore[v]+=s[i];
      posset[i].push_back(v);
    }
  }
  sortscore = minscore;
  sort(all(sortscore));
  int ans = 0;
  for (int i = 0; i < n; i++) {
    int d = sortscore[0];
    if (minscore[i] == d)
      d = sortscore[1];
    chmax(ans,maxscore[i]-d+1);
  }
  cout<<ans<<endl;
  

}

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout<<fixed<<setprecision(20);
	while(SOLVEFIN == 0) solve();
}
accuracy: 1
time_ms: 30
memory_kb: 3876
final_rank: 10

code_snippet: #include<bits/stdc++.h>
using namespace std;

using ll =long long;

void solve(ll N,ll M){
	vector<ll> CS(N,0);
	vector<array<ll,2>> LS(N);
	for(int i=0;i<N;i++)LS[i]={0,i};
	for(int i=0;i<M;i++){
		ll S,K;
		cin>>S>>K;
		for(int j=0;j<K;j++){
			ll c;
			cin>>c;
			c--;
			CS[c]+=S;
			if(K==1)LS[c][0]+=S;
		}
	}
	sort(LS.begin(),LS.end());
	ll an=0;
	for(int i=0;i<N;i++){
		if(LS[0][1]==i){
			an=max(an,CS[i]-LS[1][0]);
		}
		else{
			an=max(an,CS[i]-LS[0][0]);
		}
	}
	cout<<an+1<<"\n";
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

	ll N,M;
	while(cin>>N>>M,N+M!=0)solve(N,M);
}
accuracy: 1
time_ms: 20
memory_kb: 3712
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;

#define rep(i, n) for(ll i = 0; i < (n); ++i)
#define all(A) A.begin(), A.end()

bool chmin(auto &a, auto b) {return a > b ? a = b, 1 : 0;}
bool chmax(auto &a, auto b) {return a < b ? a = b, 1 : 0;}

template<class T>
void print(vector<T> a) {
  cout << "[";
  for(auto x : a) {
    cout << x << ", ";
  }
  cout << "]" << endl;
}

int main() {
  
  while(1) {
    ll N, M; cin >> N >> M;
    if(N + M == 0) break;
    vector<ll> mx(N, 0), mn(N, 0);
    rep(i, M) {
      ll s, k; cin >> s >> k;
      bool flg = (k == 1);
      rep(j, k) {
        ll c; cin >> c;
        c--;
        mx[c] += s;
        if(flg) mn[c] += s;
      }
    }
    // print(mx);
    // print(mn);
    
    ll res = 0;
    ll i0 = max_element(all(mx)) - mx.begin();
    ll i1 = min_element(all(mn)) - mn.begin();
    rep(j, N) {
      if(i0 != j) chmax(res, mx[i0] - mn[j]);
      if(i1 != j) chmax(res, mx[j] - mn[i1]);
    }
    cout << res + 1 << '\n';
  }
  
  return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 3452
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define ll long long

int solve(){
  ll n,m,s,k,c;cin>>n>>m;
  if(n==0&&m==0)return 0;

  vector<ll> maxscore(n,0),minscore(n,0),d(n);
  for(int i=0;i<m;i++){
    cin>>s>>k;
    if(k==1){
      cin>>c;
      minscore.at(c-1)+=s;
      maxscore.at(c-1)+=s;
      continue;
    }
    for(int j=0;j<k;j++){
      cin>>c;
      maxscore.at(c-1)+=s;
    }
  }
  map<ll,ll> ma;
  for(int i=0;i<n;i++)ma[i]=minscore[i];
  sort(minscore.begin(),minscore.end());
  for(int i=0;i<n;i++){
    if(minscore[0]!=ma[i])d[i]=maxscore[i]-minscore[0];
    else d[i]=maxscore[i]-minscore[1];
  }
  sort(d.begin(),d.end());
  cout<<d[n-1]+1<<endl;
  return 1;
}

int main(){
	while(solve()){};
}
accuracy: 1
time_ms: 120
memory_kb: 4308
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;

#include <atcoder/all>
using namespace atcoder;

using ll = long long;
using mint = modint998244353;

bool solve() {
    int N, M;
    cin >> N >> M;
    if(N == 0 && M == 0) return false;
    vector<ll> S(M);
    vector<set<int>> G(M);
    for(int i = 0; i < M; i++) {
        int k;
        cin >> S[i] >> k;
        while(k--) {
            int x;
            cin >> x;
            G[i].insert(x - 1);
        }
    }
    int ans = 0;
    for(int i = 0; i < N; i++) {
        int s = 0;
        vector<int> a(N);
        for(int j = 0; j < M; j++) {
            if(G[j].count(i)) {
                s += S[j];
            }else if(G[j].size() == 1) {
                a[*G[j].begin()] += S[j];
            }
        }
        a[i] = s;
        int m = *min_element(a.begin(), a.end());
        // cout << "# " << i + 1 << " " << s << " " << m << endl;
        ans = max(ans, s - m + 1);
    }
    cout << ans << endl;
    return true;
}

int main() {
    while(solve());
}
accuracy: 1
time_ms: 2470
memory_kb: 7808
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;

vector<int> top2(vector<int> A, bool greater) {
    vector<int> ord(A.size());
    iota(ord.begin(), ord.end(), 0);
    sort(ord.begin(), ord.end(), [&] (int i, int j) {
        return greater ? A[i] > A[j] : A[i] < A[j];
    });
    return vector<int>{ord[0], ord[1]};
}

bool solve() {
    int N, M;
    cin >> N >> M;
    if (N == 0 && M == 0) return false;
    vector<int> S(M), Smax(N, 0), Smin(N, 0);
    for (int i = 0; i < M; i++) {
        int K;
        cin >> S[i] >> K;
        vector<int> A(K);
        for (int j = 0; j < K; j++) cin >> A[j], A[j]--;
        if (K == 1) Smin[A[0]] += S[i];
        for (int a : A) Smax[a] += S[i];
    }
    int ans = 0;
    for (int i : top2(Smax, true)) {
        for (int j : top2(Smin, false)) {
            if (i != j) ans = max(ans, Smax[i] - Smin[j] + 1);
        }
    }
    cout << ans << '\n';
    return true;
}

int main() {
    while (solve()) {}
}
accuracy: 1
time_ms: 70
memory_kb: 3840
final_rank: 8

code_snippet: // AOJ 2825 - Quiz
// JAG Practice Contest for Japan Domestic 2017 - Problem C

#include <bits/stdc++.h>
using namespace std;

int main() {
  while(true) {
    int N, M; cin >> N >> M;
    if(N == 0) break;
    vector<int> correct(N+1), wrong(N+1);
    for(int i=0; i<M; ++i) {
      int S, K; cin >> S >> K;
      for(int j=0; j<K; ++j) {
        int C; cin >> C;
        correct[C] += S;
        if(K == 1) wrong[C] += S;
      }
    }
    int ans = 0;
    for(int i=1; i<=N; ++i) {
      for(int j=1; j<=N; ++j) {
        if(i != j) ans = max(ans, correct[i] - wrong[j]);
      }
    }
    cout << ans + 1 << endl;
  }
}
accuracy: 1
time_ms: 730
memory_kb: 3440
final_rank: 11

code_snippet: #if __has_include(<yoniha/all.h>)
#include <yoniha/all.h>
using namespace atcoder;
#else
#include <bits/stdc++.h>
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
#endif
using namespace std;

#define int long long
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for(int i = (int)(n - 1); i >= 0; i--)
template <typename T> bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template <typename T> bool chmin(T &a,const T &b){if(a>b){a=b;return true;}return false;}

/*
using mint = modint;
using vm = vector<mint>;
using vvm = vector<vector<mint>>;
*/

void solve(int n, int m){
  vector s(m, 0ll);
  vector c(m, vector(0, 0ll));
  rep(i, m){
    cin >> s.at(i);
    int k; cin >> k;
    c.at(i).resize(k);
    for(auto&& cij : c.at(i)) cin >> cij, cij--;
  }
  int ans = 0;
  // i番目の人が最大得点を得る
  rep(i, n){
    int ith_score = 0;
    vector min_score(n, 0ll);
    rep(j, m){
      auto&& cj = c.at(j);
      if(cj.size() == 1) min_score.at(cj.at(0)) += s.at(j);
      if(binary_search(all(cj), i)) ith_score += s.at(j);
    }
    int min_min = 1ll << 60;
    rep(j, n) if(j != i) chmin(min_min, min_score.at(j));
    chmax(ans, ith_score - min_min + 1);
  }
  cout << ans << '\n';
}

signed main(){
  while(true){
    int n, m; cin >> n >> m;
    if(n == 0) return 0;
    solve(n, m);
  }
}
accuracy: 1
time_ms: 1670
memory_kb: 4480
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i, n) for (ll i = 0; i < (n); i++)

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	for (; ; ) {
		ll n, m; cin >> n >> m;
		if (n == m and n == 0) return 0;
		vector<pair<ll, set<ll>>> ps(m);
		REP(i, m) {
			ll s, k; cin >> s >> k;
			vector<ll> tmp(k);
			for (ll& e : tmp) cin >> e, e--;
			set<ll> tmps;
			for (const ll e : tmp) tmps.emplace(e);
			ps[i] = make_pair(s, tmps);
		}

		vector<ll> min_s(n, 0);
		REP(i, m) {
			if (ps[i].second.size() == 1) {
				min_s[*begin(ps[i].second)] += ps[i].first;
			}
		}
		vector<pair<ll, ll>> min_idx(n);
		REP(i, n) {
			min_idx[i] = make_pair(min_s[i], i);
		}
		ranges::sort(min_idx);
		ranges::reverse(min_idx);

		ll ans = 0;
		REP(i, n) {
			ll sum = 0;
			REP(j, m) {
				if (ps[j].second.count(i)) {
					sum += ps[j].first;
				}
			}
			if (min_idx.back().second == i) {
				sum -= prev(prev(end(min_idx)))->first;
			} else {
				sum -= min_idx.back().first;
			}
			ans = max(ans, sum);
		}
		cout << ans + 1 << '\n';
	}
}
accuracy: 1
time_ms: 1670
memory_kb: 7936
final_rank: 19

code_snippet: #pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
//#include <atcoder/all>
#include <bitset>
#include <cmath>
#include <deque>
#include <exception>
#include <fstream>
#include <iostream>
#include <map>
#include <mutex>
#include <queue>
#include <ratio>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
// using namespace atcoder;
// using mint = modint1000000007;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
#define drep(i, n) for (int i = n - 1; i >= 0; i--)
#define srep(i, j, n) for (int i = j; i < n; i++)
#define sqrep(i, n) for (ll i = 1; i * i <= n; i++)
#define INF (int)(1e9 + 1e8)
#define LINF (ll)(1e18 + 1e17)
#define all(a) a.begin(), a.end()
#define eb emplace_back
#define mkp make_pair
#define mkt make_tuple
using ull = unsigned long long;
using vi = vector<int>;
using vd = vector<double>;
using vl = vector<ll>;
using vi2 = vector<vi>;
using vd2 = vector<vd>;
using vl2 = vector<vl>;
using vi3 = vector<vi2>;
using vl3 = vector<vl2>;
using vs = vector<string>;
using pi = pair<int, int>;
using tp = pair<int, pi>;
// using vm = vector<mint>;
// using vm2 = vector<vm>;
// using vm3 = vector<vm2>;

template <class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template <class T> inline void show_vector(vector<T>& a) {
    for (int i = 0; i < a.size(); i++) {
        cout << a[i] << " ";
    }
    cout << endl;
}

template <class T> inline void show_vector2(vector<vector<T>>& a) {
    for (int i = 0; i < a.size(); i++) {
        show_vector(a[i]);
    }
    cout << endl;
}

template <class T, class U> inline void show_map(map<T, U>& a) {
    for (pair<T, U> p : a) {
        cout << p.first << ": " << p.second << endl;
    }
}
template <class T, class U> inline void cerr_map(map<T, U>& a) {
    for (pair<T, U> p : a) {
        cerr << p.first << ": " << p.second << endl;
    }
}
template <class T, class U> inline void show_pair(pair<T, U>& a) {
    cout << a.first << " " << a.second << endl;
}
template <class T, class U> inline void cerr_pair(pair<T, U>& a) {
    cerr << a.first << " " << a.second << endl;
}
template <class T> inline void show_set(set<T>& a) {
    for (T i : a) {
        cout << i << " ";
    }
    cout << endl;
}
template <class T> inline void cerr_set(set<T>& a) {
    for (T i : a) {
        cerr << i << " ";
    }
    cerr << endl;
}

inline void yn(bool a) {
    if (a)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
}

ll ncr[100][100];

void calc_ncr() {
    ncr[0][0] = 1;
    for (int i = 0; i < 40 - 1; i++) {
        for (int j = 0; j <= i; j++) {
            ncr[i + 1][j] += ncr[i][j];
            ncr[i + 1][j + 1] += ncr[i][j];
        }
    }
}

// struct S {
//     mint a;
// };
// struct F {
//     int b;
// };
//  using S = int;
// S op(S a, S b) { return {a.a + b.a}; }
// S eee() { return {0}; }
// S mapping(F f, S x) { return {x.a + f.b}; }
// F composition(F f, F g) { return {f.b + g.b}; }
// F id() { return {0}; }
// using SEG = segtree<S, op, eee>;
//  using LAZY_SEG = lazy_segtree<S, op, eee, F, mapping, composition, id>;
void show_seg(int n) // SEG s, int n)
{
    rep(i, n) {
        // cout << s.get(i) << " ";
    }
    cout << endl;
}
struct UnionFind {
    vector<int> par;  // 親ノード
    vector<int> rank; // ランク

    UnionFind(int n = 1) { init(n); }

    void init(int n = 1) {
        par.resize(n);
        rank.resize(n);
        for (int i = 0; i < n; ++i)
            par[i] = i, rank[i] = 0;
    }

    int root(int x) {
        if (par[x] == x) {
            return x;
        } else {
            int r = root(par[x]);
            return par[x] = r;
        }
    }

    bool issame(int x, int y) { return root(x) == root(y); }

    bool merge(int x, int y) {
        x = root(x);
        y = root(y);
        if (x == y) return false;
        if (rank[x] < rank[y]) swap(x, y);
        if (rank[x] == rank[y]) ++rank[x];
        par[y] = x;
        return true;
    }
};

ll gcd_ext(ll a, ll b, ll& x, ll& y) {
    x = 1;
    ll x_next = 0;
    ll r = a;
    ll r_next = b;
    while (r_next != 0) {
        ll tmp = x_next;
        x_next = x - x_next * (r / r_next);
        x = tmp;

        tmp = r_next;
        r_next = r % r_next;
        r = tmp;
    }
    y = (r - a * x) / b;
    return r;
}
template <class T> inline void cerr_vector(vector<T>& a) {
    for (int i = 0; i < a.size(); i++) {
        cerr << a[i] << " ";
    }
    cerr << endl;
}

template <class T> inline void cerr_vector2(vector<vector<T>>& a) {
    for (int i = 0; i < a.size(); i++) {
        cerr_vector(a[i]);
    }
    cerr << endl;
}
int popcount(ull a) {
    int res = 0;
    while (a > 0) {
        if (a & 1) { res++; }
        a >>= 1;
    }
    return res;
}

ll pow_ll(ll b, ll k, ll g) {
    ll res = 1;
    ll tmp = b;
    while (k > 0) {
        if (k & 1) {
            /*
            if (k != 0 && res > __LONG_LONG_MAX__ / tmp)
            {
                return -1;
            }*/
            res *= tmp;

            res %= g;
        }
        k >>= 1;
        // todo: 消す
        /*
        if (k != 0 && tmp > __LONG_LONG_MAX__ / tmp)
        {
            return -1;
        }*/
        tmp *= tmp;

        tmp %= g;
    }
    return res;
}
using PQP = priority_queue<pair<ll, int>, vector<pair<ll, int>>,
                           greater<pair<ll, int>>>;
using P = pair<ll, ll>;
using vp = vector<P>;
using P3 = pair<ll, P>;
using vp3 = vector<P3>;
using P4 = pair<P3, int>;
using vp2 = vector<vp>;
using T = tuple<int, int, int, int>;
using t3 = tuple<ll, ll, ll>;
using vt = vector<T>;
vi dx = {0, 1, 0, -1, 1, 1, -1, -1};
vi dy = {1, 0, -1, 0, 1, -1, 1, -1};

int main() {

    while (1) {
        int n, m;
        cin >> n >> m;
        if (n == 0) break;
        vi s(m);
        vi k(m);
        vi2 c(m);

        vp p(n);
        vi p2(n);
        rep(i, n) p[i].second = i;
        rep(i, m) {
            cin >> s[i];
            cin >> k[i];
            c[i].resize(k[i]);
            rep(j, k[i]) {
                cin >> c[i][j];
                c[i][j]--;
                p2[c[i][j]] += s[i];
            }
            if (k[i] == 1) { p[c[i][0]].first += s[i]; }
        }
        sort(all(p));
        ll ans = 0;
        rep(i, n) {
            if (p[0].second != i) {
                chmax(ans, p2[i] - p[0].first + 1);
            } else {
                chmax(ans, p2[i] - p[1].first + 1);
            }
        }
        cout << ans << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 3992
final_rank: 13

code_snippet: #include <algorithm>
#include <array>
#include <bitset>
#include <cctype>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <ranges>
#include <regex>
#include <set>
#include <span>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// #include <atcoder/dsu>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define all(a) a.begin(), a.end()
using namespace std;

template <class T> inline bool chmin(T &a, T b) {
	if (a > b) {
		a = b;
		return true;
	}
	return false;
}
template <class T> inline bool chmax(T &a, T b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
}

template <class T1, class T2>
std::ostream &operator<<(std::ostream &out, const pair<T1, T2> &A) {
	cout << "{" << A.first << "," << A.second << "}";
	return out;
}

template <class T1, class T2>
std::ostream &operator<<(std::ostream &out, const map<T1, T2> &M) {
	for (const auto &A : M) {
		cout << "{" << A.first << "," << A.second << "}";
	}
	return out;
}

template <class T1>
std::ostream &operator<<(std::ostream &out, const set<T1> &M) {
	cout << "{";
	for (const auto &A : M) {
		cout << A << ", ";
	}
	cout << "}" << endl;
	return out;
}

template <class T1>
std::ostream &operator<<(std::ostream &out, const multiset<T1> &M) {
	cout << "{";
	for (const auto &A : M) {
		cout << A << ", ";
	}
	cout << "}" << endl;
	return out;
}

template <class T>
std::ostream &operator<<(std::ostream &out, const vector<T> &A) {
	for (const T &a : A) {
		cout << a << " ";
	}
	return out;
}

void print() { cout << endl; }

template <typename Head, typename... Tail> void print(Head H, Tail... T) {
	cout << H << " ";
	print(T...);
}

template <class T> std::istream &operator>>(std::istream &in, vector<T> &A) {
	for (T &a : A) {
		std::cin >> a;
	}
	return in;
}

using ll = long long;
// using mint = modint1000000007;
// using PII = pair<int, int>;
// using PLL = pair<ll, ll>;
// using Graph = vector<vector<int>>;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr ll LINF = numeric_limits<ll>::max() / 2;

// ランレングス圧縮
// イテレータを受け取る
// verify: https://atcoder.jp/contests/abc380/submissions/60002447
template <typename T, typename Iterator>
vector<pair<T, int>> RLE(Iterator begin, Iterator end) {
	vector<pair<T, int>> res;
	for (auto itr = begin; itr != end; ++itr) {
		if (res.empty() || res.back().first != *itr) {
			res.emplace_back(*itr, 1);
		} else {
			res.back().second++;
		}
	}
	return res;
}

// 座標圧縮
// unordered_mapが使えない場合はmapに変更しよう
// https://atcoder.jp/contests/abc213/submissions/60002695
template <typename T> unordered_map<T, int> compress(vector<T> &X) {
	auto tmp = X;
	ranges::sort(tmp);
	tmp.erase(unique(tmp.begin(), tmp.end()), tmp.end());
	unordered_map<T, int> res;
	for (int i = 0; i < (int)tmp.size(); i++) {
		res[tmp[i]] = i;
	}
	return res;
}

bool solve() {
	// ここからスタート
	int N, M;
	cin >> N >> M;
	if (N == 0)
		return false;
	vector<ll> must(N, 0); // その人しか取れない
	vector<ll> can(N, 0);  // 取れるだけ取った場合
	rep(i, M) {
		int s;
		cin >> s;
		int k;
		cin >> k;
		rep(j, k) {
			int c;
			cin >> c;
			c--;
			// cが解ける
			if (k == 1)
				must[c] += s;
			can[c] += s;
		}
	}
	ll mx = 0;
	rep(a, N) rep(b, N) {
		if (a == b)
			continue;
		// aが最小bが最大
		chmax(mx, can[b] - must[a]);
	}
	cout << mx + 1 << endl;
	return true;
}

int main(void) {
	std::cin.tie(0)->sync_with_stdio(0);
	while (solve())
		;
}
accuracy: 1
time_ms: 680
memory_kb: 3840
final_rank: 15

code_snippet: /***************************
* Todaka Haruto            *
* Problem: Bamboo Blossoms *
* 2024/8/9                 *
***************************/

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
#define rep(i,n) for(int i = 0; i < n; i++)

int main() {
    while(!cin.eof()) {
        int n,m;
        cin >> n >> m;
        if(n == 0 && m == 0) return 0;
        vector<int> problem (m);
        vector<int> s(m);
        vector<int> k(m);
        vector<vector<int> > c (m);

        rep(i,m) {
            cin >> s[i] >> k[i];
            rep(j,k[i]) {
                int tmp_c;
                cin >> tmp_c;
                c[i].push_back(tmp_c);
            }
        }

        int min, max;
        int count = n;
        while(count > 0) {
            vector<int> sum_point (n);
            rep(i,m) {
                if(problem[i] != 0) continue;
                rep(j, k[i]) {
                    sum_point[c[i][j]-1] += s[i];
                }
            }
            int max_id = distance(sum_point.begin(), max_element(sum_point.begin(), sum_point.end()));
            rep(i,m) {
                rep(j,k[i]) {
                    if(max_id == c[i][j]-1) {
                        problem[i] = 1;
                    }
                }
            }
            if(count == n) {
                max = sum_point[max_id];
            }
            if(count == 1) {
                min = sum_point[max_id];
            }
            count--;
        }
        cout << max - min + 1 << endl;

    }
    return 0;
}
accuracy: 1
time_ms: 7210
memory_kb: 3928
final_rank: 18

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

#include <unistd.h>

namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf


uint32_t pil = 0, pir = 0, por = 0;

struct Pre {
    char num[10000][4];
    constexpr Pre() : num() {
        for (int i = 0; i < 10000; i++) {
            int n = i;
            for (int j = 3; j >= 0; j--) {
                num[i][j] = n % 10 | '0';
                n /= 10;
            }
        }
    }
} constexpr pre;

inline void load() {
    memmove(ibuf, ibuf + pil, pir - pil);
    pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
    pil = 0;
    if (pir < SZ)
        ibuf[pir++] = '\n';
}

inline void flush() {
    fwrite(obuf, 1, por, stdout);
    por = 0;
}

void rd(char &c) {
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
}

void rd(string &x) {
    x.clear();
    char c;
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
    do {
        x += c;
        if (pil == pir)
            load();
        c = ibuf[pil++];
    } while (!isspace(c));
}

template <typename T> void rd_real(T &x) {
    string s;
    rd(s);
    x = stod(s);
}

template <typename T> void rd_integer(T &x) {
    if (pil + 100 > pir)
        load();
    char c;
    do
        c = ibuf[pil++];
    while (c < '-');
    bool minus = 0;
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (c == '-') {
            minus = 1, c = ibuf[pil++];
        }
    }
    x = 0;
    while ('0' <= c) {
        x = x * 10 + (c & 15), c = ibuf[pil++];
    }
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (minus)
            x = -x;
    }
}

void rd(int &x) {
    rd_integer(x);
}
void rd(ll &x) {
    rd_integer(x);
}
void rd(i128 &x) {
    rd_integer(x);
}
void rd(uint &x) {
    rd_integer(x);
}
void rd(ull &x) {
    rd_integer(x);
}
void rd(u128 &x) {
    rd_integer(x);
}
void rd(double &x) {
    rd_real(x);
}
void rd(long double &x) {
    rd_real(x);
}

template <class T, class U> void rd(pair<T, U> &p) {
    return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T> void rd_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
        auto &x = std::get<N>(t);
        rd(x);
        rd_tuple<N + 1>(t);
    }
}
template <class... T> void rd(tuple<T...> &tpl) {
    rd_tuple(tpl);
}

template <size_t N = 0, typename T> void rd(array<T, N> &x) {
    for (auto &d : x)
        rd(d);
}
template <class T> void rd(vector<T> &x) {
    for (auto &d : x)
        rd(d);
}

void read() {}
template <class H, class... T> void read(H &h, T &...t) {
    rd(h), read(t...);
}

void wt(const char c) {
    if (por == SZ)
        flush();
    obuf[por++] = c;
}
void wt(const string s) {
    for (char c : s)
        wt(c);
}
void wt(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++)
        wt(s[i]);
}

template <typename T> void wt_integer(T x) {
    if (por > SZ - 100)
        flush();
    if (x < 0) {
        obuf[por++] = '-', x = -x;
    }
    int outi;
    for (outi = 96; x >= 10000; outi -= 4) {
        memcpy(out + outi, pre.num[x % 10000], 4);
        x /= 10000;
    }
    if (x >= 1000) {
        memcpy(obuf + por, pre.num[x], 4);
        por += 4;
    } else if (x >= 100) {
        memcpy(obuf + por, pre.num[x] + 1, 3);
        por += 3;
    } else if (x >= 10) {
        int q = (x * 103) >> 10;
        obuf[por] = q | '0';
        obuf[por + 1] = (x - q * 10) | '0';
        por += 2;
    } else
        obuf[por++] = x | '0';
    memcpy(obuf + por, out + outi + 4, 96 - outi);
    por += 96 - outi;
}

template <typename T> void wt_real(T x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << double(x);
    string s = oss.str();
    wt(s);
}

void wt(int x) {
    wt_integer(x);
}
void wt(ll x) {
    wt_integer(x);
}
void wt(i128 x) {
    wt_integer(x);
}
void wt(uint x) {
    wt_integer(x);
}
void wt(ull x) {
    wt_integer(x);
}
void wt(u128 x) {
    wt_integer(x);
}
void wt(double x) {
    wt_real(x);
}
void wt(long double x) {
    wt_real(x);
}

template <class T, class U> void wt(const pair<T, U> val) {
    wt(val.first);
    wt(' ');
    wt(val.second);
}
template <size_t N = 0, typename T> void wt_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
        if constexpr (N > 0) {
            wt(' ');
        }
        const auto x = std::get<N>(t);
        wt(x);
        wt_tuple<N + 1>(t);
    }
}
template <class... T> void wt(tuple<T...> tpl) {
    wt_tuple(tpl);
}
template <class T, size_t S> void wt(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}
template <class T> void wt(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}

void print() {
    wt('\n');
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    wt(head);
    if (sizeof...(Tail))
        wt(' ');
    print(forward<Tail>(tail)...);
}
void __attribute__((destructor)) _d() {
    flush();
}
} // namespace fastio


using fastio::flush;
using fastio::print;
using fastio::read;

inline void first(bool i = true) {
    print(i ? "first" : "second");
}
inline void Alice(bool i = true) {
    print(i ? "Alice" : "Bob");
}
inline void Takahashi(bool i = true) {
    print(i ? "Takahashi" : "Aoki");
}
inline void yes(bool i = true) {
    print(i ? "yes" : "no");
}
inline void Yes(bool i = true) {
    print(i ? "Yes" : "No");
}
inline void No() {
    print("No");
}
inline void YES(bool i = true) {
    print(i ? "YES" : "NO");
}
inline void NO() {
    print("NO");
}
inline void Yay(bool i = true) {
    print(i ? "Yay!" : ":(");
}
inline void Possible(bool i = true) {
    print(i ? "Possible" : "Impossible");
}
inline void POSSIBLE(bool i = true) {
    print(i ? "POSSIBLE" : "IMPOSSIBLE");
}

/**
 * @brief Fast IO
 */

template <unsigned mod = 1000000007> struct fp {
    unsigned v;
    static constexpr int get_mod() {
        return mod;
    }
    constexpr unsigned inv() const {
        assert(v != 0);
        int x = v, y = mod, p = 1, q = 0, t = 0, tmp = 0;
        while (y > 0) {
            t = x / y;
            x -= t * y, p -= t * q;
            tmp = x, x = y, y = tmp;
            tmp = p, p = q, q = tmp;
        }
        if (p < 0)
            p += mod;
        return p;
    }
    constexpr fp(ll x = 0) : v(x >= 0 ? x % mod : (mod - (-x) % mod) % mod) {}
    fp operator-() const {
        return fp() - *this;
    }
    fp pow(ull t) {
        fp res = 1, b = *this;
        while (t) {
            if (t & 1)
                res *= b;
            b *= b;
            t >>= 1;
        }
        return res;
    }
    fp &operator+=(const fp &x) {
        if ((v += x.v) >= mod)
            v -= mod;
        return *this;
    }
    fp &operator-=(const fp &x) {
        if ((v += mod - x.v) >= mod)
            v -= mod;
        return *this;
    }
    fp &operator*=(const fp &x) {
        v = ull(v) * x.v % mod;
        return *this;
    }
    fp &operator/=(const fp &x) {
        v = ull(v) * x.inv() % mod;
        return *this;
    }
    fp operator+(const fp &x) const {
        return fp(*this) += x;
    }
    fp operator-(const fp &x) const {
        return fp(*this) -= x;
    }
    fp operator*(const fp &x) const {
        return fp(*this) *= x;
    }
    fp operator/(const fp &x) const {
        return fp(*this) /= x;
    }
    bool operator==(const fp &x) const {
        return v == x.v;
    }
    bool operator!=(const fp &x) const {
        return v != x.v;
    }
    friend istream &operator>>(istream &is, fp &x) {
        return is >> x.v;
    }
    friend ostream &operator<<(ostream &os, const fp &x) {
        return os << x.v;
    }
};

template <unsigned mod> void rd(fp<mod> &x) {
    fastio::rd(x.v);
}
template <unsigned mod> void wt(fp<mod> x) {
    fastio::wt(x.v);
}

template <typename T> T Inv(ll n) {
    static const int md = T::get_mod();
    static vector<T> buf({0, 1});
    assert(n > 0);
    n %= md;
    while (SZ(buf) <= n) {
        int k = SZ(buf), q = (md + k - 1) / k;
        buf.push_back(buf[k * q - md] * q);
    }
    return buf[n];
}

template <typename T> T Fact(ll n, bool inv = 0) {
    static const int md = T::get_mod();
    static vector<T> buf({1, 1}), ibuf({1, 1});
    assert(n >= 0 and n < md);
    while (SZ(buf) <= n) {
        buf.push_back(buf.back() * SZ(buf));
        ibuf.push_back(ibuf.back() * Inv<T>(SZ(ibuf)));
    }
    return inv ? ibuf[n] : buf[n];
}

template <typename T> T nPr(int n, int r, bool inv = 0) {
    if (n < 0 || n < r || r < 0)
        return 0;
    return Fact<T>(n, inv) * Fact<T>(n - r, inv ^ 1);
}
template <typename T> T nCr(int n, int r, bool inv = 0) {
    if (n < 0 || n < r || r < 0)
        return 0;
    return Fact<T>(n, inv) * Fact<T>(r, inv ^ 1) * Fact<T>(n - r, inv ^ 1);
}
template <typename T> T nHr(int n, int r, bool inv = 0) {
    return nCr<T>(n + r - 1, r, inv);
}

/**
 * @brief Modint
 */

int main() {
while(1) {
    int N, M;
    cin >> N >> M;
    if (N == 0 && M == 0) return 0;
    vector<pair<int,int>> V(N);
    rep(i,0,M) {
        int S, K;
        cin >> S >> K;
        vector<int> C(K);
        rep(i,0,K) cin >> C[i], C[i]--, V[C[i]].first += S;
        if (K == 1) V[C[0]].second += S;
    }
    sort(V.begin(), V.end(), greater<pair<int,int>>());
    int ANS = -inf;
    rep(i,0,2) {
        rep(j,0,N) {
            if (i == j) continue;
            chmax(ANS, V[i].first - V[j].second);
        }
    }
    cout << ANS + 1 << endl;
}
}
accuracy: 1
time_ms: 90
memory_kb: 3532
final_rank: 4