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>
using namespace std;
struct log{
  int T,s,d;
  bool operator<(const log &a)const{
    return T<a.T;
  }
};
const int MAX_N=30000;
int parent[MAX_N];
int depth[MAX_N];

void init(int n){
  for(int i=0;i<n;i++){
    parent[i]=i;
    depth[i]=0;
  }
}

int find(int x){
  if(parent[x]==x){
    return x;
  }else{
    return parent[x]=find(parent[x]);
  }
}

void unite(int x,int y){
  x=find(x);
  y=find(y);
  if(x==y)return;
  if(depth[x]<depth[y]){
    parent[x]=y;
  }else{
    parent[y]=x;
    if(depth[x]==depth[y])depth[x]++;
  }
}

bool same(int x,int y){
  return find(x)==find(y);
}

log l[30000];
int main(){
  int n,m;
  while(cin>>n>>m,n){
    init(n);
    for(int i=0;i<m;i++)
      cin>>l[i].T>>l[i].s>>l[i].d;
    sort(l,l+m);
    int ans=1;
    for(int i=0;i<m;i++){
      if(same(1,l[i].s)){
	ans++;
	if(!same(1,l[i].d))unite(1,l[i].d);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <complex>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <bitset>
#include <ctime>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cassert>
#include <cstddef>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <sstream>
#include <fstream>

using namespace std;
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define RREP(i, a) for(int (i) = (a) - 1; (i) >= 0; (i)--)
#define FORR(i, a, b) for(int (i) = (a) - 1; (i) >= (b); (i)--)
#define DEBUG(C) cerr << #C << " = " << C << endl;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<LL>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using PII = pair<int, int>;
using PDD = pair<double, double>;
using PLL = pair<LL, LL>;
using VPII = vector<PII>;
template<typename T> using VT = vector<T>;
#define ALL(a) begin((a)), end((a))
#define RALL(a) rbegin((a)), rend((a))
#define SORT(a) sort(ALL((a)))
#define RSORT(a) sort(RALL((a)))
#define REVERSE(a) reverse(ALL((a)))
#define MP make_pair
#define FORE(a, b) for (auto &&a : (b))
#define FIND(s, e) ((s).find(e) != (s).end())
#define EB emplace_back

const int INF = 1e9;
const int MOD = INF + 7;
const LL LLINF = 1e18;

/*** UnionFind????????? ***/
class UnionFind {
private:
    //uni[i]<0????????°i?????????????????§???????????°find(uni[i])??????
    vector<int> uni;
    //??°???????????°
    int SIZE;
public:

    UnionFind(int size) {
        //????????¶?????§?????¨?????????-1????¨???????????????£??????????????????
        this->uni.resize(size, -1);
        this->SIZE = size;
    }
     UnionFind() {}
     void resize(int size) {
         this->uni.resize(size, -1);
         this->SIZE = size;
     }

    //x???????????????
    int find(int x) {
        //uni[x]<0????????§x??????
        if (uni[x] < 0) return x;
        //uni[x]????????????????????\???????????¨??§??£?¶???§?????¢?´¢???????????????
        return uni[x] = find(uni[x]);
    }

    //x??¨y???????????°?????????????????°true
    bool same(int x, int y) {
        //x??¨y???????????????????????°????????°?????????????±?????????¨?????????
        return find(x) == find(y);
    }

    //x??¨y????????????????±?????????°??????????????????
    //????????????????????£???????????°true?????????
    //x??¨y????????¨?????¨????????°?????????????????°false?????????
    bool unite(int x, int y) {
        x = find(x); y = find(y);
        if (x == y) return false;
        //??¨?????±???????°????????????????????????§??????????????????????????????
        if (uni[x] > uni[y]) swap(x, y);
        //y?????°????????????x????¶????
        uni[x] += uni[y];
        //y?????????x??¨??????
        uni[y] = x;
        this->SIZE--;
        return true;
    }

    //??°???????????°?????????
    int size() {
        return this->SIZE;
    }

    //x????±?????????°???????????????????????°?????????
    int size(int x) {
        return -uni[find(x)];
    }
};

using type = tuple<int, int, int>;
void solve(int N, int M) {
    VT<type> vec(M);
    for_each(ALL(vec), [](type &e) {
        int t, s, d;scanf("%d%d%d", &t, &s, &d);
        e = make_tuple(t, s - 1, d - 1);
    });
    SORT(vec);
    UnionFind uf(N);
    FORE(e, vec) {
        int t, s, d;
        tie(t, s, d) = e;
        if (uf.same(0, s)) {
            uf.unite(s, d);
        }
    }
    cout << uf.size(0) << endl;
    return;
}

signed main(void) {
    LL n, m, p, a, b, c, x, y, z, q;
    string s;
    bool f = false;
    while (cin >> n >> m, n) {
        solve(n, m);
        //return 0;
    }
}

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

vector<pair<int, pair<int , int>>> l;

int v[20010];
int n, m, ans;


int main(void){
	int i;
	pair<int, pair<int, int>> p;
	while( cin >> n >> m, n){
		ans = 0;
		memset(l, 0, sizeof(l));
		memset(v, 0, sizeof(v));
		for(i = 0; i < m; i++){
			cin >> p.first >> p.second.first >> p.second.second;
			l.push_back(p);
		}
		sort(l.begin(), l.end());
		v[1] = 1;
		for(i = 0; i < m; i++) if(v[l[i].second.first]) v[l[i].second.second] = 1;
		for(i = 1; i <= n; i++) if(v[i]) ans++;
		cout << ans << endl;
	}
	return 0;
}