#include<bits/stdc++.h>
using namespace std;
int main(){
long long t=0,n=0,m=0,o=0,x=0,k=0;
bool b=false;
cin>>t;
while(t--){
cin>>n>>m;
while(m--){
cin>>o>>x;
if(o==1){
b=true;
k++;
}
else{
b=false;
}
}
}
cout<<k<<endl;
if(b==false){
cout<<"-1";
}
return 0;
}