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 <iostream>
#include <algorithm>
#include <vector>

using namespace std;

struct Packet {
  int t, s, d;

  Packet() = default;
  Packet(int t, int s, int d): t(t), s(s), d(d) { }

  bool operator<(const Packet& p) const {
    return t < p.t;
  }
};

int main() {
  while (true) {    
    int N, M;
    cin >> N >> M;

    if (N + M == 0) {
      break;
    }

    bool infected[20001] = { true };

    vector<Packet> packet(M);
    for (int i = 0; i < M; i++) {
      int t, s, d;
      cin >> t >> s >> d;
      packet[i] = Packet(t, s - 1, d - 1);
    }
    sort(packet.begin(), packet.end());

    for (int i = 0; i < M; i++) {
      if (infected[packet[i].s]) {
        infected[packet[i].d] = true;
      }
    }

    int ans = 0;
    for (int i = 0; i < N; i++) {
      if (infected[i]) {
        ans++;
      }
    }

    cout << ans << endl;
  }

  return 0;
}

C++: #include <iostream>
using namespace std;
struct set{
  int t;
  int s;
  int d;
};
int main(){
  int n,m,i,j,cnt,source,destination;
  struct set data[20001];
  while(1){
    cin >> n >> m;
    if(n==0&&m==0) break;
    for(i=0;i<m;i++){
      cin >> data[i].t >> data[i].s >> data[i].d;
    }
    for(i=0;i<m;i++){
      for(j=1;j<m;j++){
        if(data[i].t<data[i-1].t) swap(data[i],data[i-1]);
      }
    }
    source=data[0].s;
    destination=data[0].d;
    if(source==1) cnt=2;
    if(destination==1) cnt=1;
    for(i=1;i<m;i++){
      if(destination==data[i].s){
	cnt++;
        destination=data[i].d;
      }
    }
    cout << cnt << endl;
  }
  return 0;
}

C++: #include<stdio.h>
#include<algorithm>
#include<bitset>
const int N=20001;
struct S{int t,s,d;}a[N];
int C(S l,S r){return l.t<r.t;}
int main()
{
	int n,m,i;
	std::bitset<N>b;
	while(scanf("%*d%d",&m),m)
	{
		for(i=1;i<=m;++i)scanf("%d%d%d",&a[i].t,&a[i].s,&a[i].d);
		std::sort(a+1,a+1+m,C);
		b.reset();
		n=b[1]=1;
		for(i=1;i<=m;++i)if(b[a[i].s]&&!b[a[i].d])b[a[i].d]=1,++n;
		printf("%d\n",n);
	}
	return 0;
}

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;
struct P {
	long long time, from, to;
};
bool operator<(P a, P b) { return a.time < b.time; }
bool operator>(P a, P b) { return a.time > b.time; }
bool a[20001];
int main() {
	long long n, m;
	while (cin >> n >> m&&n != 0 && m != 0) {
		for (long long i = 1; i <= n; i++) a[i] = false;
		a[1] = true;
		priority_queue<P, vector<P>, greater<P>>p;
		for (long long i = 0; i < m; i++) {
			long long t, s, d;
			cin >> t >> s >> d;
			p.push(P{ t,s,d });
		}
		long long sum = 1;
		while (!p.empty()) {
			P t = p.top(); p.pop();
			if (a[t.from]) {
				if (!a[t.to]) sum++;
				a[t.to] = true;
			}
		}
		cout << sum << endl;
	}
}

C++: #include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <map>
using namespace std;

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define REPI(i, s, e) for (int i = (s); i <= (e); i++)
#define rep(i, n) REP(i, 0, n)
#define repi(i, n) REPI(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

#define dump(x) (cout << #x << " = " << x << endl)
#define dump2(x, y) (cout << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl)
#define dump3(x, y, z) (cout << "(" << #x << ", " << #y << ", " << #z << ") = (" << x << ", " << y << ", "<< z << ")" << endl)

typedef long long ll;
typedef pair<int, int> pii;

int N, M;

class node{
public:
	int t, s, d;
	node(int t, int s, int d):t(t), s(s), d(d){}

	bool operator<(const node& r) const {
		return t < r.t;
	}
};

#define MAX 20002
int prev[MAX];
bool has_parent[MAX];

int main(void)
{
	while (cin >> N >> M, N|M) {
		if (N == 1) {
			cout << 1 << endl;
			continue;
		}

		vector<node> ns;

		rep(i, M) {
			int t, s, d;
			cin >> t >> s >> d;
			ns.push_back(node(t, --s, --d));
		}
		sort(ALL(ns));

		memset(prev, -1, sizeof(prev));
		fill(has_parent, has_parent+MAX, false);
		rep(i, ns.size()) {
			node n = ns[i];
			prev[n.d] = n.s;
		}

		rep(i, N)
			if (prev[i] != -1)
				has_parent[prev[i]] = true;
		rep(i, N)
			if (prev[i] != -1 && !has_parent[i])
				cout << i+1 << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(void){
    int n, m;
    while(cin>>n>>m){
        if(n+m==0) break;
        pair<int,pair<int, int>> com[20010];
        for(int i=0; i<m; i++){
            int a, b, c;
            cin >> a >> b >> c;
            com[i]=make_pair(a, make_pair(b, c));
        }
        sort(com, com+m);
        bool infected[20010];
        fill(infected, infected+(n+1), false);
        infected[1]=true;
        for(int i=0; i<m; i++){
            if(infected[com[i].second.first]) infected[com[i].second.second]=true; 
        }
        int count=0;
        for(int i=1; i<=n; i++){
            if(infected[i]) count++;
        }
        cout << count << "\n";
    }
    
}