import javax.swing.plaf.multi.MultiViewportUI;
import java.io.*;
import java.util.*;

public class Validate implements Runnable {
    private static final int MAXN = (int) 1e5;
    private static final int MAXVAL = (int) 1e9;

    int[] l, r;
    private boolean[] vis;
    private int cnt;

    public void run() {
        StrictScanner inf = new StrictScanner(System.in);

        int n = inf.nextInt();
        ensureLimits(n, 1, MAXN, "n is out of range");
        int t = inf.nextInt();
        ensureLimits(t, 1, MAXVAL, "t is out of range");
        inf.nextLine();

        l = new int[n];
        r = new int[n];

        for (int i = 0; i < n; i++) {
            l[i] = inf.nextInt();
            ensureLimits(l[i], 0, MAXVAL, "l[" + i + "] is out of range");
            r[i] = inf.nextInt();
            ensureLimits(r[i], 0, MAXVAL, "r[" + i + "] is out of range");
            int x = inf.nextInt();
            ensureLimits(x, 1, MAXVAL, "a[" + i + "] is out of range");
            l[i]--;
            r[i]--;
            inf.nextLine();
        }

        inf.close();

        vis = new boolean[n];
        cnt = 0;
        dfs(0);

        ensure(cnt == n, "input should be connected tree");
    }

    private void dfs(int v) {
        if (v == -1) return;
        ensure(!vis[v], "input should be tree");
        vis[v] = true;
        cnt++;
        dfs(l[v]);
        dfs(r[v]);
    }

    public static void main(String[] args) {
        new Thread(null, new Validate(), "", 1 << 26).start();
    }

    public class StrictScanner {
        private final BufferedReader in;
        private String line = "";
        private int pos;
        private int lineNo;

        public StrictScanner(InputStream source) {
            in = new BufferedReader(new InputStreamReader(source));
            nextLine();
        }

        public void close() {
            ensure(line == null, "Extra data at the end of file");
            try {
                in.close();
            } catch (IOException e) {
                throw new AssertionError("Failed to close with " + e);
            }
        }

        public void nextLine() {
            ensure(line != null, "EOF");
            ensure(pos == line.length(), "Extra characters on line " + lineNo);
            try {
                line = in.readLine();
            } catch (IOException e) {
                throw new AssertionError("Failed to read line with " + e);
            }
            pos = 0;
            lineNo++;
        }

        public String next() {
            ensure(line != null, "EOF");
            ensure(line.length() > 0, "Empty line " + lineNo);
            if (pos == 0)
                ensure(line.charAt(0) > ' ', "Line " + lineNo + " starts with whitespace");
            else {
                ensure(pos < line.length(), "Line " + lineNo + " is over");
                ensure(line.charAt(pos) == ' ', "Wrong whitespace on line " + lineNo);
                pos++;
                ensure(pos < line.length(), "Line " + lineNo + " is over");
                ensure(line.charAt(0) > ' ', "Line " + lineNo + " has double whitespace");
            }
            StringBuilder sb = new StringBuilder();
            while (pos < line.length() && line.charAt(pos) > ' ')
                sb.append(line.charAt(pos++));
            return sb.toString();
        }

        public int nextInt() {
            String s = next();
            ensure(s.length() == 1 || s.charAt(0) != '0', "Extra leading zero in number " + s + " on line " + lineNo);
            ensure(s.charAt(0) != '+', "Extra leading '+' in number " + s + " on line " + lineNo);
            try {
                return Integer.parseInt(s);
            } catch (NumberFormatException e) {
                throw new AssertionError("Malformed number " + s + " on line " + lineNo);
            }
        }

        public double nextDouble() {
            String s = next();
            ensure(s.length() == 1 || s.startsWith("0.") || s.charAt(0) != '0', "Extra leading zero in number " + s + " on line " + lineNo);
            ensure(s.charAt(0) != '+', "Extra leading '+' in number " + s + " on line " + lineNo);
            try {
                return Double.parseDouble(s);
            } catch (NumberFormatException e) {
                throw new AssertionError("Malformed number " + s + " on line " + lineNo);
            }
        }
    }

    void ensure(boolean b, String message) {
        if (!b) {
            throw new AssertionError(message);
        }
    }

    void ensureLimits(int n, int from, int to, String name) {
        ensure(from <= n && n <= to, name + " must be from " + from + " to " + to);
    }

}
