/*输入的第一行包含两个整数n, m,分别表示小岛的个数和桥的数量。
接下来m行,每行三个整数a, b, t,分别表示该座桥连接a号和b号两个小岛,能使用t天。小岛的编号从1开始递增。
数据规模和约定
对于100%的数据,1< =n< =10000,1< =m< =100000,1< =a, b< =n, 1< =t< =100000。*/
//输出一个整数,表示居民们会抗议的天数。
import java.util.Scanner;
public class guowang {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n=in.nextInt(),m=in.nextInt();
int a[][]=new int[n+1][n+1];
int count[]=new int[100000];
int day=2,k=1,sum=0;
while(m>0){
a[in.nextInt()][in.nextInt()]=in.nextInt();
--m;
}
for (int i = 0; i <n+1 ; i++){
for (int j = 0; j < n+1; j++) {
while (a[i][j] >= day) {
count[day++] = 1;
break;
}
}
}
for (int h= 0; h< count.length; h++) {
if (count[h] == 1) {
sum++;
}
}
System.out.println(sum);
}
}