import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;

public class e1 {
	public static void main(String[] args) {
		Scanner read = new Scanner(System.in);
		
		int n = read.nextInt(); 
		int k = read.nextInt();
		read.nextLine();
		
		int s = 0;
		
		for(int i = 0; i < n; i++) {
			int ti = read.nextInt();
			int ai = read.nextInt();
			int bi = read.nextInt();
			read.nextLine();
			
			if( ai == 1 && bi == 1) {
				s += ti;
			}
			
		}
		
		System.out.println( s );
	}
}