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++: // clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
// #define main int main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
#define stlice(from, to) substr(from, (to) - (from) + 1)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
using namespace std;
using pii = pair<int, int>;
using vi = vector<int>;
using vd = vector<double>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vpii = vector<pii>;
using vvi = vector<vi>;
using vvb = vector<vb>;
using vvpii = vector<vpii>;
template<typename A> using fn = function<A>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int &n) { return n & 1; }
constexpr bool even(const int &n) { return !odd(n); }
void solve();
main { solve(); return 0; }
// clang-format on

class UFT {
private:
  vi data, rnk;

public:
  UFT(const int &size)
    : data(size), rnk(size) {
    iota(all(data), 0);
  }

  int root(const int &x) {
    return data[x] == x ? x : data[x] = root(data[x]);
  }

  bool same(const int &a, const int &b) {
    return root(a) == root(b);
  }

  void unite(int a, int b) {
    a = root(a), b = root(b);
    if (a == b) return;
    data[b] = a;
  }
};

void solve() {
  int n, m;
  while (cin >> n >> m, n) {
    vector<tuple<int, int, int>> v;
    while (m--) {
      int t, s, d;
      cin >> t >> s >> d;
      v.emplace_back(t, --s, --d);
    }
    sort(all(v));
    UFT uf(n);
    rep(i, v.size()) {
      int s, d;
      tie(ignore, s, d) = v[i];
      if (uf.same(0, s)) uf.unite(0, d);
    }
    int c = 0;
    rep(i, n) c += uf.same(0, i);
    cout << c << endl;
  }
}

C++: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
  
using namespace std;
  
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P> PP;
  
static const double EPS = 1e-8;
  
static const int tx[] = {0,1,0,-1};
static const int ty[] = {-1,0,1,0};

class Packet{
public:
  int time,src,dst;
  Packet(int _t,int _s,int _d) : time(_t),src(_s),dst(_d){}
  bool operator<(const Packet& p) const{
    return time < p.time;
  }
  bool operator>(const Packet& p) const{
    return time > p.time;
  }
};

int main(){
  int computer_num,send_list_num;
  while(~scanf("%d %d",&computer_num,&send_list_num)){
    if(computer_num == 0 && send_list_num == 0) break;

    vector<Packet> packets;
    for(int i=0;i<send_list_num;i++){
      int time,src,dst;
      scanf("%d %d %d",&time,&src,&dst);
      packets.push_back(Packet(time,src,dst));
    }

    sort(packets.begin(),packets.end());
    
    bool infected[20001];
    memset(infected,0,sizeof(infected));
    infected[1] = true;
    for(int i=0;i<packets.size();i++){
      if(infected[packets[i].src]){
	infected[packets[i].dst] = true;
      }
    }

    int res = 0;
    for(int i=1;i<=computer_num;i++){
      if(infected[i]) res++;
    }

    printf("%d\n",res);
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
#define INF 1000000000

bool comp(vector<int> a, vector<int> b){
    return a[0] < b[0];
}

int main(){

    while(1){
        int n, m; cin >> n >> m;
        if(n == 0) break;
        vector<vector<int> > A(m, vector<int> (3));  //[0] := t [1] := s [2] := d
        for(int i = 0; i < m; i++){
            cin >> A[i][0] >> A[i][1] >> A[i][2];
            A[i][1]--;
            A[i][2]--;
        }
        vector<bool> virus(n, false);
        virus[0] = true;
        sort(A.begin(), A.end(), comp);

        for(int i = 0; i < m; i++){
            virus[A[i][2]] = virus[A[i][2]] || virus[A[i][1]];
        }

        int ans = 0;
        for(int i = 0; i < n; i++) if(virus[i]) ans++;

        cout << ans << endl;
    }
}

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

struct po{
  int t,s,d;
};
 
int main() {

  while(1) {
    int n,m;
    cin >> n >> m;
    if(n == 0 && m == 0) break;

    po ls[20000];
    for(int i=0;i<m;i++) {
      cin >> ls[i].t >> ls[i].s >> ls[i].d;
    }

    bool sta[20001] = {};
    sta[1] = 1;
    
    for(int i=0;i<m;i++) 
      for(int j=m-1;j>i;j--) 
	if(ls[j].t < ls[j-1].t){
	  po tmp = ls[j];
	  ls[j] = ls[j-1];
	  ls[j-1] = tmp;
	}

     
    for(int i=0;i<m;i++){
      if(sta[ls[i].s] == 1) {
	sta[ls[i].d] = 1;
      }
    }
    int ans = 0;
    for(int i=1;i<=n;i++) 
      if(sta[i] == 1) ans++;

    cout << ans <<endl;
  }
    
}