import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;

alias Yoko = Tuple!(int, "h", int, "p", int, "q");

void main() {

    while (1) {
        int n, m, a;
        scan(n,m,a);

        if(!n) return;

        auto y = new Yoko[](m);

        foreach (i ; 0 .. m) {
            int hi, pi, qi;
            scan(hi,pi,qi);
            y[i] = Yoko(hi, pi, qi);
        }

        y.sort!"a > b"();

        auto am = iota(n + 1).array;

        foreach (yi ; y) {
            swap(am[yi.p], am[yi.q]);
        }

        writeln(am.countUntil(a));
    }
}


void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}


void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}