Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
)

func main() {
	io := NewIo()
	defer io.Flush()
	for {
		n, m := io.NextInt(), io.NextInt()
		if n == 0 {
			break
		}
		dat := make([]Dat, m)
		for i := 0; i < m; i++ {
			dat[i] = Dat{io.NextInt(), io.NextInt(), io.NextInt()}
		}
		sort.Slice(dat, func(i, j int) bool { return dat[i].t < dat[j].t })
		infected := make([]bool, n+1)
		infected[1] = true
		for i := 0; i < m; i++ {
			infected[dat[i].d] = infected[dat[i].d] || infected[dat[i].s]
		}
		cnt := 0
		for i := 0; i <= n; i++ {
			if infected[i] {
				cnt++
			}
		}
		io.Println(cnt)
	}
}

type Dat struct {
	t, s, d int
}

// I/O template
type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buff []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buff = append(buff, line...)
		if !isPrefix {
			break
		}
	}
	return string(buff)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	s := io.tokens[io.nextToken]
	io.nextToken++
	return s
}

func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) Println(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func (io *Io) PrintIntSlice(a []int) {
	b := make([]interface{}, 0, len(a))
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func (io *Io) PrintStringSlice(a []string) {
	b := make([]interface{}, 0, len(a))
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func Log(name string, value interface{}) {
	fmt.Fprintf(os.Stderr, "%s=%+v\n", name, value)
}

func ExampleIo() {
	io := NewIo()
	defer io.Flush()
	a := make([]int, 0, 3)
	a[0], a[1], a[2] = io.NextInt(), io.NextInt(), io.NextInt()
	io.Println(a)
	io.PrintIntSlice(a)
	Log("io", a)
}

C++: #include<cstdio>
#include<vector>
#include<algorithm>

#define	mp			make_pair
#define	rep(i,n)	for(int i=0;i<n;i++)

using namespace std;

typedef	vector<int>		vi;
typedef	pair<int,int>	pii;

class UnionFind{
	vi a;
	int root(int x){ return ~a[x]?a[x]=root(a[x]):x; }
public:
	UnionFind(int n):a(n,-1){}
	bool find(int x,int y){ return root(x)==root(y); }
	bool unite(int x,int y){ return find(x,y)?0:(a[root(y)]=root(x),1); }
};

int main(){
	for(int m,n;scanf("%d%d",&n,&m),n;){
		pair<int,pii> packet[20000];
		rep(i,m)	scanf("%d%d%d",&packet[i].first,&packet[i].second.first,&packet[i].second.second);
		sort(packet,packet+m);

		int ans=1;
		UnionFind link(n+1);
		rep(i,m){
			int src=packet[i].second.first,dst=packet[i].second.second;
			if(link.find(1,src))	ans+=link.unite(src,dst);
		}
		printf("%d\n",ans);
	}

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;

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

    int n, m;
    while (cin >> n >> m, n) {
        VV data(m, V(3));
        rep(i, m) {
            int t, s, d;
            cin >> t >> s >> d;
            s--, d--;
            data[i] = {t, s, d};
        }
        sort(all(data));

        vector<bool> infected(n, false);
        infected[0] = true;
        rep(i, m) {
            if (infected[data[i][1]]) infected[data[i][2]] = true;
        }

        int ans = 0;
        rep(i, n) {
            ans += infected[i];
        }

        cout << ans << endl;

    }
}

C++: #include<iostream>
#include<string>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<iomanip>
#include<vector>
#include<functional>
#include<algorithm>
#include<cstdio>
using namespace std;
long long a[20001];
long long inf = 1000000000;
struct P{
	long long time, pos;
};
bool operator<(P a, P b) { return a.time < b.time; }
bool operator>(P a, P b) { return a.time > b.time; }
struct B {
	long long time, to;
};
vector<B>e [200001];
int main() {
	long long n,m;
	while (cin >> n>>m&&n != 0||m!=0) {
		for (int i = 0; i <= n; i++) {
			a[i] = inf;
			e[i].clear();
		}
		for (int i = 0; i < m; i++) {
			int a, b, c;
			cin >> a >> b >> c;
			e[b].push_back(B{ a,c });
		}
		a[1] = 0;
		priority_queue<P, vector<P>, greater<P>>p;
		p.push(P{ 0,1 });
		while (!p.empty()) {
			P t = p.top(); p.pop();
			if (a[t.pos] != inf) {
				for (int i = 0; i < (int)e[t.pos].size(); i++) {
					if (a[e[t.pos][i].to] > a[t.pos] + e[t.pos][i].time) {
						a[e[t.pos][i].to] = a[t.pos] + e[t.pos][i].time;
						p.push(P{ a[e[t.pos][i].to],e[t.pos][i].to });
					}
				}
			}
		}
		int sum = 0;
		for (int i = 1; i <= n; i++) {
			if (a[i] != inf) sum++;
		}
		cout << sum << endl;
	}
}

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

using namespace std;

int main(){
	int n,m;
	while(cin >> n >> m,n&&m){
		vector<pair<int,pair<int,int>>> v;
		for(int i=0;i<m;i++){
			int t,s,d;
			cin >> t >> s >> d;
			v.emplace_back(make_pair(t,make_pair(s,d)));
		}
		bool infected[20000];
		for(int i=0;i<20000;i++) infected[i] = false;
		infected[0] = true;
		sort(v.begin(),v.end());
		for(auto x:v){
			if(infected[x.second.first-1]){
				infected[x.second.second-1] = true;
			}
		}
		int ans = 0;
		for(int i=0;i<20000;i++){
			if(infected[i]){
				//cout << i << endl;
				ans++;
			}
		}
		cout << ans << endl;
	}
	
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;

struct Data{
  
  int t,s,d;

  bool operator<(const Data& data)const{
    if(data.t != t)return t < data.t;  
    if(data.s != s)return s < data.s;
    return d < data.d;
  }
  
};
int main(){

  int N,M;
  
  while(1){

    cin >>N>>M;
    if(N==0 && M==0)break;
    
    Data data[M];
    for(int i=0 ; i < M ; i++){
      cin >>data[i].t>>data[i].s>>data[i].d;
    }
    
    sort(data,data+M);
    
    bool isinfected[N+1];
    for(int i=1 ; i <= N ; i++)isinfected[i]=false;
    isinfected[1]=true;
    
    int cnt=1;
    for(int i=0 ; i < M ; i++){
      if(isinfected[data[i].s] && !isinfected[data[i].d]){
	isinfected[data[i].d]=true;
	cnt++;
      }
    }
    
    cout <<cnt<<endl;
    
  }
  
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <set>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;
typedef long long ll;

int main()
{
    int n,m;
    while(cin >> n >> m) {
        if(n == 0 && m == 0) break;
        
        vector<pair<int,pair<int,int> > > v(m);
        rep(i,m) cin >> v[i].first >> v[i].second.first >> v[i].second.second;

        sort(v.begin(),v.end());

        bool T[20005];
        memset(T,0,sizeof(T));
        T[1] = true;
        set<int> st;
        st.insert(1);
        
        rep(i,m) {
            int time = v[i].first;
            int s = v[i].second.first;
            int t = v[i].second.second;

            if(T[s]) {
                T[t] = true;
                st.insert(t);
            }
        }
        
        cout << st.size() << endl;

    }

    return 0;
}