#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

// Max verticles
#define N 1000
// Max edges
#define M 2000

// Num of verticles
static int n;
// Num of edges
static int m;

struct edge {
    int start;
    int end;
};

static struct edge edges[M];
static struct edge *map[N+1][N+1]; // map[b][a] points to a edge a -> b

static bool visited[N+1];

static struct edge *el[M];
static int el_len = 0;

bool find_first_closed_graph(int start) {
    // printf("start=%d\n", start);
    for (int i = 0; i < m; i++) {
        if (edges[i].start == start) {
            // printf("  %d -> %d\n", edges[i].start, edges[i].end);
            el[el_len] = &edges[i];
            el_len++;

            // Found closed graph
            for (int j = 0; j < el_len-1; j++) {
                if (el[j]->start == el[el_len-1]->end) {
                    el_len -= j;
                    for (int k = 0; k < el_len; k++) {
                        el[k] = el[k+j];
                    }
                    return 1;
                }
            }

            if (find_first_closed_graph(el[el_len-1]->end)) {
                return 1;
            }
            el_len--;
        }
    }

    visited[start] = true;

    // Not found closed graph
    // printf("end\n");
    return 0;
}

struct edge *find_edge_not_used_in_graph(int *start_i, int *end_i) {
    for (int i = 0; i < el_len; i++) {
        for (int j = 0; j < el_len; j++) {
            if (j - i == 0 || j - i == 1 || (i == el_len - 1 && j == 0)) continue;
            if (map[el[j]->start][el[i]->start] != NULL) {
                *start_i = i;
                *end_i = j;
                return map[el[j]->start][el[i]->start];
            }
        }
    }

    return NULL;
}

void recreate_closed_graph(struct edge *ep, int start_i, int end_i) {
    if (end_i > start_i) {
        el[start_i] = ep;
        for (int k = 0; k < el_len - end_i; k++) {
            el[start_i+1+k] = el[end_i+k];
        }
        el_len -= (end_i - start_i - 1);
    } else {
        el[start_i] = ep;
        for (int k = end_i; k <= start_i; k++) {
            el[k-(end_i)] = el[k];
        }
        el_len = start_i - end_i + 1;
    }
}

int main(void) {
    struct edge *ep;
    bool has_closed_graph;
    int start_i, end_i;

    scanf("%d %d", &n, &m);

    for (int i = 0; i < m; i++) {
        int a, b;
        scanf("%d %d", &a, &b);
        edges[i].start = a;
        edges[i].end = b;
        map[b][a] = &edges[i];
    }

    for (int i = 1; i <= n; i++) {
        if (!visited[i]) {
            has_closed_graph = find_first_closed_graph(i);
            if (has_closed_graph) {
                break;
            }
        }
    }

    if (has_closed_graph) {
        for (;;) {
            // for (int i = 0; i < el_len; i++) {
            //     printf("%d -> %d\n", el[i]->start, el[i]->end);
            // }
            // printf("\n");

            ep = find_edge_not_used_in_graph(&start_i, &end_i);
            if (ep != NULL) {
                // printf("use %d -> %d, start_i=%d, end_i=%d\n", ep->start, ep->end, start_i, end_i);
                recreate_closed_graph(ep, start_i, end_i);
            } else {
                printf("%d\n", el_len);
                for (int i = 0; i < el_len; i++) {
                    printf("%d\n", el[i]->start);
                    // printf("%d -> %d\n", el[i].start, el[i].end);
                }
                break;
            }
        }
    } else {
        printf("%d\n", -1);
    }

    return 0;
}


Measured memory_bytes: 6784