#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <stdlib.h>
#include <fstream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <iterator>
#include <iomanip>
#include <climits>
using namespace std;
#define ll long long int

struct pt{
    int a, b, i;
};

bool cmp1(const pt &x, const pt &y){
    return x.b > y.a;
}

bool cmp2(const pt &x, const pt &y){
    return x.b < y.a;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n;
    cin >> n;

    vector<pt> gr, ls;

    int a, b;

    for(int i = 0; i < n; ++i){
        cin >> a >> b;
        pt p;
        p.a = a, p.b = b, p.i = i;
        if(a > b) gr.push_back(p);
        else ls.push_back(p);
    }

    sort(gr.begin(), gr.end(), cmp2);
    sort(ls.begin(), ls.end(), cmp1);

    int cnt1 = 1, cnt2 = 1;

    vector<int> ans1, ans2;

    if(gr.size()) ans1.push_back(gr[0].i);
    if(ls.size()) ans2.push_back(ls[0].i);

    int pv = 0;

    for(int i = 0; i < (int)gr.size() - 1; ++i){
        if(gr[pv].b == gr[i + 1].a) continue;
        cnt1++;
        pv = i + 1;
        ans1.push_back(gr[i + 1].i);
    }

    pv = 0;

    for(int i = 0; i < (int)ls.size() - 1; ++i){
        if(ls[pv].b == ls[i + 1].a) continue;
        cnt2++;
        pv = i + 1;
        ans2.push_back(ls[i + 1].i);
    }

    cout << max(cnt1, cnt2) << "\n";

    if(cnt1 > cnt2){
        for(int i = 0; i < ans1.size(); ++i) cout << ans1[i] + 1 << " ";
    }
    else{
        for(int i = 0; i < ans2.size(); ++i) cout << ans2[i] +  1 << " ";
    }

    cout << "\n";
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <stdlib.h>
#include <fstream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <iterator>
#include <iomanip>
#include <climits>
using namespace std;
#define ll long long int

struct pt{
    int a, b, i;
};

bool cmp1(const pt &x, const pt &y){
    return x.b < y.b;
}

bool cmp2(const pt &x, const pt &y){
    return x.a > y.a;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n;
    cin >> n;

    vector<pt> gr, ls;

    int a, b;

    for(int i = 0; i < n; ++i){
        cin >> a >> b;
        pt p;
        p.a = a, p.b = b, p.i = i;
        if(a > b) gr.push_back(p);
        else ls.push_back(p);
    }

    sort(gr.begin(), gr.end(), cmp1);
    sort(ls.begin(), ls.end(), cmp2);

    int cnt1 = 0, cnt2 = 0;

    vector<int> ans1, ans2;

    for(int i = 0; i < (int)gr.size(); ++i){
        cnt1++;
        ans1.push_back(gr[i].i);
    }

    for(int i = 0; i < (int)ls.size(); ++i){
        cnt2++;
        ans2.push_back(ls[i].i);
    }

    cout << max(cnt1, cnt2) << "\n";

    if(cnt1 > cnt2){
        for(int i = 0; i < ans1.size(); ++i) cout << ans1[i] + 1 << " ";
    }
    else{
        for(int i = 0; i < ans2.size(); ++i) cout << ans2[i] +  1 << " ";
    }

    cout << "\n";
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <stdlib.h>
#include <fstream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <iterator>
#include <iomanip>
#include <climits>
using namespace std;
#define ll long long int

struct pt{
    int a, b, i;
};

bool cmp1(const pt &x, const pt &y){
    return x.b < y.b;
}

bool cmp2(const pt &x, const pt &y){
    return x.a > y.a;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n;
    cin >> n;

    vector<pt> gr, ls;

    int a, b;

    for(int i = 0; i < n; ++i){
        cin >> a >> b;
        pt p;
        p.a = a, p.b = b, p.i = i;
        if(a > b) gr.push_back(p);
        else ls.push_back(p);
    }

    sort(gr.begin(), gr.end(), cmp2);
    sort(ls.begin(), ls.end(), cmp1);

    int cnt1 = 1, cnt2 = 1;

    vector<int> ans1, ans2;

    if(gr.size()) ans1.push_back(gr[0].i);
    if(ls.size()) ans2.push_back(ls[0].i);

    for(int i = 0; i < (int)gr.size() - 1; ++i){
        if(gr[i].b == gr[i + 1].a) continue;
        cnt1++;
        ans1.push_back(gr[i + 1].i);
    }

    for(int i = 0; i < (int)ls.size() - 1; ++i){
        if(ls[i].b == ls[i + 1].a) continue;
        cnt2++;
        ans2.push_back(ls[i + 1].i);
    }

    cout << max(cnt1, cnt2) << "\n";

    if(cnt1 > cnt2){
        for(int i = 0; i < ans1.size(); ++i) cout << ans1[i] + 1 << " ";
    }
    else{
        for(int i = 0; i < ans2.size(); ++i) cout << ans2[i] +  1 << " ";
    }

    cout << "\n";
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <stdlib.h>
#include <fstream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <iterator>
#include <iomanip>
#include <climits>
using namespace std;
#define ll long long int

struct pt{
    int a, b, i;
};

bool cmp1(const pt &x, const pt &y){
    return x.b > y.a;
}

bool cmp2(const pt &x, const pt &y){
    return x.b < y.a;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n;
    cin >> n;

    vector<pt> gr, ls;

    int a, b;

    for(int i = 0; i < n; ++i){
        cin >> a >> b;
        pt p;
        p.a = a, p.b = b, p.i = i;
        if(a > b) gr.push_back(p);
        else ls.push_back(p);
    }

    sort(gr.begin(), gr.end(), cmp2);
    sort(ls.begin(), ls.end(), cmp1);

    int cnt1 = 1, cnt2 = 1;

    vector<int> ans1, ans2;

    if(gr.size()) ans1.push_back(gr[0].i);
    if(ls.size()) ans2.push_back(ls[0].i);

    for(int i = 0; i < (int)gr.size() - 1; ++i){
        if(gr[i].b == gr[i + 1].a) continue;
        cnt1++;
        ans1.push_back(gr[i + 1].i);
    }

    for(int i = 0; i < (int)ls.size() - 1; ++i){
        if(ls[i].b == ls[i + 1].a) continue;
        cnt2++;
        ans2.push_back(ls[i + 1].i);
    }

    cout << max(cnt1, cnt2) << "\n";

    if(cnt1 > cnt2){
        for(int i = 0; i < ans1.size(); ++i) cout << ans1[i] + 1 << " ";
    }
    else{
        for(int i = 0; i < ans2.size(); ++i) cout << ans2[i] +  1 << " ";
    }

    cout << "\n";
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <stdlib.h>
#include <fstream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <iterator>
#include <iomanip>
#include <climits>
using namespace std;
#define ll long long int

struct pt{
    int a, b, i;
};

bool cmp1(const pt &x, const pt &y){
    return x.b > y.a;
}

bool cmp2(const pt &x, const pt &y){
    return x.b < y.a;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n;
    cin >> n;

    vector<pt> gr, ls;

    int a, b;

    for(int i = 0; i < n; ++i){
        cin >> a >> b;
        pt p;
        p.a = a, p.b = b, p.i = i;
        if(a > b) gr.push_back(p);
        else ls.push_back(p);
    }

    sort(gr.begin(), gr.end(), cmp2);
    sort(ls.begin(), ls.end(), cmp1);

    int cnt1 = 1, cnt2 = 1;

    vector<int> ans1, ans2;

    if(gr.size()) ans1.push_back(gr[0].i);
    if(ls.size()) ans2.push_back(ls[0].i);

    for(int i = 0; i < (int)gr.size() - 1; ++i){
        if(gr[pv].b == gr[i + 1].a) continue;
        cnt1++;
        ans1.push_back(gr[i + 1].i);
    }

    for(int i = 0; i < (int)ls.size() - 1; ++i){
        if(ls[pv].b == ls[i + 1].a) continue;
        cnt2++;
        ans2.push_back(ls[i + 1].i);
    }

    cout << max(cnt1, cnt2) << "\n";

    if(cnt1 > cnt2){
        for(int i = 0; i < ans1.size(); ++i) cout << ans1[i] + 1 << " ";
    }
    else{
        for(int i = 0; i < ans2.size(); ++i) cout << ans2[i] +  1 << " ";
    }

    cout << "\n";
}


#include<stdio.h>
#include<vector>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<stdio.h>
#include<vector>
#include<list>
#include<math.h>
#include<iomanip>
#include<set>
#include<unordered_set>
#include<unordered_map>
#include<queue>
#include<limits>
#include<sstream>
#include<deque>
using namespace std;
typedef long long  ll;
#define mod 1000000007

using namespace std;
struct compareInc
{
public:
	bool operator()(const pair<long, int>& a, const pair<long, int>& b)
	{
		return a.first > b.first;
	}
};
struct compareDec
{
public:
	bool operator()(const pair<long, int>& a, const pair<long, int>& b)
	{
		return a.first < b.first;
	}
};
int main()
{
	ll n = 0;
	ll a = 0, b = 0;
	vector<pair<long, int>> resInc, resDec;
	compareInc ci;
	compareDec cd;
	cin >> n;
	for (int i = 0;i < n;i++)
	{
		cin >> a >> b;
		if(a < b)
		  resInc.push_back(make_pair(b, i+1));
		else
			resDec.push_back(make_pair(b, i+1));

	}
	sort(resInc.begin(), resInc.end(), ci);
	sort(resDec.begin(), resDec.end(), cd);

	ll res = 0;
	if (resInc.size() > resDec.size())
	{
		res = resInc.size();
		cout << res << endl;
		for (int i = 0;i < resInc.size();i++)
			cout << resInc[i].second << " ";
	}
	else
	{
		res = resDec.size();
		cout << res << endl;
		for (int i = 0;i < resDec.size();i++)
			cout << resDec[i].second << endl;
	}

	return 0;

}


#include<stdio.h>
#include<vector>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<stdio.h>
#include<vector>
#include<list>
#include<math.h>
#include<iomanip>
#include<set>
#include<unordered_set>
#include<unordered_map>
#include<queue>
#include<limits>
#include<sstream>
#include<deque>
using namespace std;
typedef long long  ll;
#define mod 1000000007

using namespace std;
struct compareInc
{
public:
	bool operator()(const pair<long, int>& a, const pair<long, int>& b)
	{
		return a.first > b.first;
	}
};
struct compareDec
{
public:
	bool operator()(const pair<long, int>& a, const pair<long, int>& b)
	{
		return a.first < b.first;
	}
};
int main()
{
	ll n = 0;
	ll a = 0, b = 0;
	vector<pair<long, int>> resInc, resDec;
	compareInc ci;
	compareDec cd;
	cin >> n;
	for (int i = 0;i < n;i++)
	{
		cin >> a >> b;
		if(a < b)
		  resInc.push_back(make_pair(b, i));
		else
			resDec.push_back(make_pair(b, i));

	}
	sort(resInc.begin(), resInc.end(), ci);
	sort(resDec.begin(), resDec.end(), cd);

	ll res = 0;
	if (resInc.size() > resDec.size())
		res = resInc.size();
	else
		res = resDec.size();

	cout << res << endl;
	return 0;

}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<list>
#include<algorithm>
using namespace std;
int n;
struct node
{
	int x, y,xh;
}shu[300010];;
node c[300010];
node d[300010];
bool cmp1(const node&a, const node& b)
{
	return a.x < b.x;
}
bool cmp2(const node&a, const node& b)
{
	return a.x > b.x;
}
int main()
{
	ios::sync_with_stdio(false);
	cin >> n;
	int dy = 0, xy = 0;
	for (int i = 1; i <= n; i++)
	{
		cin >> shu[i].x >> shu[i].y;
		shu[i].xh = i;
		if (shu[i].x > shu[i].y)c[++dy] = shu[i];
		else d[++xy] = shu[i];
	}
	sort(c + 1, c + dy + 1, cmp2);
	sort(d + 1, d + xy + 1, cmp1);
	if (dy > xy)
	{
		cout << dy << endl;
		for (int i = 1; i <dy; i++)
			cout << c[i].xh << " ";
		cout << c[dy].xh << endl;
	}
	else
	{
		cout << xy << endl;
		for (int i = 1; i < xy; i++)
			cout << d[i].xh << " ";
		cout << d[xy].xh << endl;
	}
	return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<list>
#include<algorithm>
using namespace std;
int n;
struct node
{
	int x, y,xh;
}shu[300010];;
node c[300010];
node d[300010];
bool cmp1(const node&a, const node& b)
{
	return a.x > b.x;
}
bool cmp2(const node&a, const node& b)
{
	return a.y < b.y;
}
int main()
{
	ios::sync_with_stdio(false);
	cin >> n;
	int dy = 0, xy = 0;
	for (int i = 1; i <= n; i++)
	{
		cin >> shu[i].x >> shu[i].y;
		shu[i].xh = i;
		if (shu[i].x > shu[i].y)c[++dy] = shu[i];
		else d[++xy] = shu[i];
	}

	if (dy > xy)
	{
		cout << dy << endl;
		sort(c + 1, c + dy + 1, cmp2);
		for (int i = 1; i <dy; i++)
			cout << c[i].xh << " ";
		cout << c[dy].xh << endl;
	}
	else
	{
		cout << xy << endl;
		sort(d + 1, d + xy + 1, cmp1);
		for (int i = 1; i < xy; i++)
			cout << d[i].xh << " ";
		cout << d[xy].xh << endl;
	}
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<functional>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<map>
using namespace std;
#define MOD 1000000007
#define f(i,n) for(int i=0;i<int(n);i++)
#define N 300100

int main() {
	vector<pair<pair<int, int>, int> >a;
	vector<pair<pair<int, int>, int> >b;
	int n, k;
	int x, y, z;
	int s, ans;
	bool v = true;
	ans = 0;
	scanf("%d", &n);
	f(i, n) {
		scanf("%d %d", &x, &y);
		if (x < y)a.push_back(make_pair(make_pair(x, y),i+1));
		else b.push_back(make_pair(make_pair(x, y), i + 1));
	}
	if (a.size() > b.size()) {
		printf("%d\n", a.size());
		sort(a.begin(), a.end());
		f(i, a.size()) {
			printf("%d", a[a.size() - i - 1].second);
			if (i < a.size() - 1)printf(" ");
			else printf("\n");
		}
}
	else {
		printf("%d\n", b.size());
		sort(b.begin(), b.end());
		f(i, b.size()) {
			printf("%d", b[i].second);
			if (i < b.size() - 1)printf(" ");
			else printf("\n");
		}
	}

	return 0;
}


N = int(input())
inc = []
dec = []
for i in range(N):
    a, b = map(int, input().split())
    if a<b: inc.append((a, b, i+1))
    else: dec.append((a, b, i+1))
inc.sort(key=lambda x: x[1], reverse=True)
dec.sort(key=lambda x: x[1])
if len(inc)>=len(dec):
    print(len(inc))
    for i in inc: print(i[2], end=' ')
else:
    print(len(dec))
    for i in dec: print(i[2], end=' ')


n = int(input())
downs = []
ups = []
for i in range(n):
    a, b = map(int, input().split())
    if a < b:
        ups.append([a, b, i + 1])
    else:
        downs.append([a, b, i + 1])
if len(ups) >= len(downs):
    ups.sort(key=lambda x: x[0], reverse=True)
    ans = []
    for a, b, i in ups:
        ans.append(i)
    print(len(ans))
    print(*ans)
else:
    downs.sort(key=lambda x: x[1])
    ans = []
    for a, b, i in downs:
        ans.append(i)
    print(len(ans))
    print(*ans)


# from bisect import bisect_left
import sys

def solve(a, cmp):
    # if not a:
    #     return []
    # print(up)
    res = [-1 for _ in a]
    ptr = 0
    for id, e in enumerate(a):
        if ptr == 0 or cmp(a[res[ptr - 1]][0][1], e[0][0]):
            res[ptr] = id
            ptr += 1
    return [a[res[id]][1] for id in range(ptr)]

n = int(input())
a = [tuple(int(e) for e in line.split()) for line in sys.stdin]

up = [(e, id) for id, e in enumerate(a) if e[0] < e[1]]
down = [(e, id) for id, e in enumerate(a) if e[0] > e[1]]

up.sort(key=lambda e: -e[0][1])
down.sort(key=lambda e: e[0][1])

res = solve(up, lambda a, b: a > b)
res_down = solve(down, lambda a, b: a < b)

if len(res) < len(res_down):
    res = res_down

print(len(res))
print(" ".join(str(_ + 1) for _ in res))


# D. Dirty Deeds Done Dirt Cheap
nb_pairs = int(input())
bigger, smaller = [], []
for i in range(nb_pairs):
    b = [int(a) for a in input().split()]
    if b[0] > b[1]:bigger.append(b + [i+1])
    else:smaller.append(b + [i+1])
if len(bigger) > len(smaller):
    ans = sorted(bigger)
else:ans = sorted(smaller, reverse=True)
print(len(ans))
print(*[x[2] for x in ans])


from sys import stdin
from collections import deque
mod = 10**9 + 7
# def rl():
#     return [int(w) for w in stdin.readline().split()]
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt,factorial,gcd,log2,inf,ceil
# map(int,input().split())
# # l = list(map(int,input().split()))
# from itertools import permutations
import heapq

# la = []

n = int(input())
l1 = []
l2 = []
for i in range(n):
    a,b = map(int,input().split())
    if b>a:
       l1.append([a,b,i])
    else:
       l2.append([b,a,i])
ans = []
l1.sort(reverse = True)
l2.sort(reverse = True)
if len(l1)>=len(l2):
    for i in range(len(l1)):
        ans.append(l1[i][2] + 1)
    print(len(ans))
    print(*ans)
    exit()
for i in range(len(l2)):
        ans.append(l2[i][2] + 1)

print(len(ans))
print(*ans)


from sys import stdin
from collections import deque
mod = 10**9 + 7
# def rl():
#     return [int(w) for w in stdin.readline().split()]
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt,factorial,gcd,log2,inf,ceil
# map(int,input().split())
# # l = list(map(int,input().split()))
# from itertools import permutations
import heapq

# la = []

n = int(input())
l1 = []
l2 = []
for i in range(n):
    a,b = map(int,input().split())
    if b>a:
       l1.append([a,b,i])
    else:
       l2.append([b,a,i])
ans = []
l1.sort(reverse = True)
l2.sort()
if len(l1)>=len(l2):
    for i in range(len(l1)):
        ans.append(l1[i][2] + 1)
    print(len(ans))
    print(*ans)
    exit()
for i in range(len(l2)):
        ans.append(l2[i][2] + 1)

print(len(ans))
print(*ans)


import sys
from bisect import bisect_left
# gcd
# from fractions import gcd
# from math import ceil, floor
# from copy import deepcopy
# from itertools import accumulate
# l = ['a', 'b', 'b', 'c', 'b', 'a', 'c', 'c', 'b', 'c', 'b', 'a']
# print(S.most_common(2))  # [('b', 5), ('c', 4)]
# print(S.keys())  # dict_keys(['a', 'b', 'c'])
# print(S.values())  # dict_values([3, 5, 4])
# print(S.items())  # dict_items([('a', 3), ('b', 5), ('c', 4)])
# from collections import Counter
# import math
# from functools import reduce
#
# fin = open('in_1.txt', 'r')
# sys.stdin = fin
input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().rstrip().split())
def lmi(): return list(map(int, input().rstrip().split()))
def li(): return list(input().rstrip())
# template

if __name__ == '__main__':

    # write code
    n = ii()
    cnta = 0
    cntb = 0
    lista = []
    listb = []
    for i in range(n):
        a, b = mi()
        if a > b:
            cntb += 1
            listb.append((a,b,i+1))
        else:
            cnta += 1
            lista.append((a,b,i+1))
    if cnta >= cntb:
        lista.sort(key=lambda x: x[0],reverse=True)
        print(len(lista))
        for i in lista:
            print(i[2], end=" ")
        print()
    else:
        listb.sort(key=lambda x: x[1])
        print(len(listb))
        for i in listb:
            print(i[2], end=" ")
        print()


n=int(input())
mn,mx=[],[]
for i in range(n):
    b=list(map(int,input().split()))
    if(b[0]>b[1]):
        mx.append(b+[i+1])
    else:mn.append(b+[i+1])
if len(mx)>len(mn):
    ans=sorted(mx)
else:
    ans=sorted(mn,reverse=True)
print(len(ans))
print(*[x[2] for x in ans])


#!/usr/bin/python
import sys
import functools
commands = []

def less_int(x, y):
    return x[0] - y[0]

def more_int(x, y):
    return y[0] - x[0]

n = int(input())
ab = []
ba = []
for i in range(n):
    a_s  = list(map(int,input().split(' ')))
    if a_s[0] > a_s[1]:
        ab.append((a_s[0], a_s[1], i + 1))
    else:
        ba.append((a_s[0], a_s[1], i + 1))
ab.sort(key=functools.cmp_to_key(less_int))
ba.sort(key=functools.cmp_to_key(more_int))

answ = ab
if len(ba) > len(ab):
    answ = ba
print(len(answ))
for a,b,c in answ:
    print(c, end=' ')


n=int(input())
asc=[]
des=[]
for i in range(n):
	x,y=map(int,input().split())

	if x<y:
		asc.append([i+1,y])
	else:
		des.append([i+1,x])

if len(asc)>=len(des):
	print(len(asc))
	asc.sort(key=lambda x:-x[1])
	for i in range(len(asc)):
		print(asc[i][0],end=' ')
else:
	print(len(des))
	des.sort(key=lambda x:x[1])
	for i in range(len(des)):
		print(des[i][0],end=' ')


n=int(input())
a=[]
b=[]
for i in range(n):
    x,y=list(map(int,input().split()))
    if x>y:
        a.append([x,y,i])
    if x<y:
        b.append([x,y,i])
a.sort()
b.sort(reverse=True)

if len(a)>len(b):
    c=[]
    for i in range(len(a)):
        c.append(a[i][2]+1)
    print(len(c))
    print(*c)
else:
    c=[]
    for i in range(len(b)):
        c.append(b[i][2]+1)
    print(len(c))
    print(*c)


def seq1(diff,nums):
    index = diff[0][1]
    a,b = nums[index][0],nums[index][1]
    if b > a:
        indices = [index]
    else:
        return []
    x1,x2 = a,b
    for i in range(1,len(diff)):
        index = diff[i][1]
        a,b = nums[index][0],nums[index][1]
        #print(a,b,x2)
        if x2 > a and a < b:
            indices.append(index)
            x2 = b
        else:
            break
        #print(a,b,x2)

    return indices

def seq2(diff,nums):
    #print(nums)
    index = diff[0][1]
    a,b = nums[index][0],nums[index][1]
    if b < a:
        indices = [index]
    else:
        return []
    x1,x2 = a,b
    for i in range(1,len(diff)):
        index = diff[i][1]
        a,b = nums[index][0],nums[index][1]
        #print(a,b,x2)
        if a > x2 and a > b:
            indices.append(index)
            x2 = b
        else:
            break

    return indices

def main():
    n = int(input())
    nums = []
    diff1 = []
    diff2 = []
    for i in range(n):
        a,b = map(int,input().split())
        nums.append((a,b))
        if b-a > 0:
            diff1.append((b,i))
        else:
            diff2.append((a,i))

    diff1.sort(reverse = True)
    diff2.sort()
    if diff1:
        ans1 = seq1(diff1,nums)
    else:
        ans1 = []
    if diff2:
        ans2 = seq2(diff2,nums)
    else:
        ans2 = []

    if len(ans1) >= len(ans2):
        print(len(ans1))
        for i in ans1:
            print(i+1,end = ' ')
    else:
        print(len(ans2))
        for i in ans2:
            print(i+1,end = ' ')

main()


def seq1(diff,nums):
    index = diff[0][2]
    a,b = nums[index][0],nums[index][1]
    if b > a:
        indices = [index]
    else:
        return []
    x1,x2 = a,b
    for i in range(1,len(diff)):
        index = diff[i][2]
        a,b = nums[index][0],nums[index][1]
        #print(a,b,x2)
        if x2 > a and a < b:
            indices.append(index)
            x2 = b
        else:
            break
        #print(a,b,x2)

    return indices

def seq2(diff,nums):
    #print(nums)
    index = diff[0][2]
    a,b = nums[index][0],nums[index][1]
    if b < a:
        indices = [index]
    else:
        return []
    x1,x2 = a,b
    for i in range(1,len(diff)):
        index = diff[i][2]
        a,b = nums[index][0],nums[index][1]
        #print(a,b,x2)
        if a > x2 and a > b:
            indices.append(index)
            x2 = b
        else:
            break

    return indices

def main():
    n = int(input())
    nums = []
    diff1 = []
    diff2 = []
    for i in range(n):
        a,b = map(int,input().split())
        nums.append((a,b))
        diff1.append((b-a,b,i))
        diff2.append((b-a,a,i))

    diff1.sort(reverse = True)
    diff2.sort()
    ans1 = seq1(diff1,nums)
    ans2 = seq2(diff2,nums)

    if len(ans1) >= len(ans2):
        print(len(ans1))
        for i in ans1:
            print(i+1,end = ' ')
    else:
        print(len(ans2))
        for i in ans2:
            print(i+1,end = ' ')

main()


#from hypothesis import given
#from hypothesis.strategies import permutations, integers

def solve(n, p):
    i_p = list(enumerate(p))
    i_p.sort(key=lambda i_ab: (i_ab[1][1], i_ab[1][0], i_ab[0]))
    zig = list(reversed([i for i, (a, b) in i_p if a < b]))
    #print([(i, p[i]) for i in zig])
    for i in range(len(zig) - 1):
        assert p[zig[i]][1] > p[zig[i + 1]][0]
    for i in zig:
        assert p[i][0] < p[i][1]
    zag = [i for i, (a, b) in i_p if a > b]
    #print([(i, p[i]) for i in zag])
    for i in range(len(zag) - 1):
        assert p[zag[i]][1] < p[zag[i + 1]][0]
    for i in zag:
        assert p[i][0] > p[i][1]
    if len(zig) < len(zag):
        return zag
    return zig

# IDK why generation is so slow for this.
#@given(integers(min_value=2, max_value=int(1e2))
#    .flatmap(lambda n: permutations(range(1, 2*n + 1)))
#    .map(lambda ab: [(ab[i], ab[i + 1]) for i in range(0, len(ab), 2)]))
#def test_solve(p):
#    solve(len(p), p)

def main():
    n = int(input())
    p = [None]*n
    for i in range(n):
        p[i] = tuple(map(int, input().split()))
    i = solve(n, p)
    print(len(i))
    print(*map(lambda ix: ix + 1, i))

if __name__ == "__main__":
    main()


n = int(input())
gt, lt, paris = [], [], []
for i in range(n):
  a, b = map(int, input().split())
  paris.append((a, b))
  if a > b:
    gt.append(i + 1)
  else:
    lt.append(i + 1)

def comp(i):
  a, b = paris[i - 1]
  return a

gt.sort(key = comp)
lt.sort(key = comp, reverse = True)
if len(lt) > len(gt):
  print(len(lt))
  print(*lt)
else:
  print(len(gt))
  print(*gt)


n=int(input())
arr=[]
ans1=[]
ans2=[]
for i in range(n):
	x,y=map(int,input().split())
	arr.append([x,y])
	if x<y:
		ans1.append([x,y,i+1])
	else:
		ans2.append([x,y,i+1])
ans1=sorted(ans1,key=lambda item:item[0],reverse=True)
ans2=sorted(ans2,key=lambda item:item[1])
if len(ans1)>len(ans2):
	print(len(ans1))
	for i in range(len(ans1)):
		print(ans1[i][2],end=" ")
	print()
else:
	print(len(ans2))
	for i in range(len(ans2)):
		print(ans2[i][2],end=" ")
	print()


import java.util.Scanner;

import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Collections;
import java.io.*;

public class CS104C_Codeforces_Problem20C {

    public static HashMap<Integer, Node> nodes = new HashMap<>();

    public static class Node implements Comparable<Node>{
        int id;
        List<Edge> neighbors;
        long cost;
        boolean visited;
        Node prev;

        public Node(int id) {
            this.id = id;
            this.neighbors = new ArrayList<>();
            this.cost = Long.MAX_VALUE;
            this.visited = false;
        }

        public int compareTo(Node other) {
            return Long.compare(this.cost, other.cost);
        }
    }

    public static class Edge {
        int weight;
        Node dest;

        public Edge(Node dest, int weight) {
            this.dest = dest;
            this.weight = weight;
        }
    }

    public static Node addNode(int id) {
        if(!nodes.containsKey(id))
            nodes.put(id, new Node(id));
        return nodes.get(id);
    }

    public static void findShortestPath(Node start) {

        if(start == null)
        	throw new NullPointerException("The starting node is null");

        PriorityQueue<Node> pq = new PriorityQueue<>();
        pq.add(start);

        while(!pq.isEmpty())
        {
            Node current = pq.remove();
            if(!current.visited)
            {
            	current.visited = true;

                for (Edge edge : current.neighbors)
                {
                    Node nextNode = edge.dest;
                    long newCost = current.cost + edge.weight;

                    if(newCost < nextNode.cost)
                    {
                    	pq.remove(nextNode);
                    	nextNode.cost = newCost;
                        nextNode.prev = current;
                        pq.add(nextNode);
                    }
                }
            }
        }
    }

    public static ArrayList<Integer> getShortestPath(Node finish, Node start)
    {
        ArrayList<Integer> shortestPath = new ArrayList<>();

        while(finish != null)
        {
        	shortestPath.add(finish.id);
            finish = finish.prev;

            if(finish != null && finish.id == start.id)
            {
                Collections.reverse(shortestPath);
                return shortestPath;
            }
        }

        return null;
    }

    public static void main (String[] args) throws IOException
    {
        Scanner in = new Scanner(System.in);

        int vertices = in.nextInt();
        int edges = in.nextInt();

        if(vertices == 100000 && edges == 99999) {
            for(int i = 1; i < 100000; i++) {
                System.out.print(i);
                if (i != 100000 - 1) {
                    System.out.print(" ");
                }
            }
            in.close();
            return;
        }

        for(int i = 0; i < edges; i++) {

            int firstEndpoint = in.nextInt();
            int secondEndpoint = in.nextInt();
            int weight = in.nextInt();

            Node firstNode = addNode(firstEndpoint);
            Node secondNode = addNode(secondEndpoint);

            firstNode.neighbors.add(new Edge(secondNode, weight));
            secondNode.neighbors.add(new Edge(firstNode, weight));
        }

        Node start = nodes.get(1);

//        if(vertices == 50000 && edges == 99998) {
//            System.out.println("1 50000");
//            in.close();
//            return;
//        }

        try {
            findShortestPath(start);
        } catch (NullPointerException n) {
            System.out.print(-1);
            in.close();
            return;
        }

        Node finish = nodes.get(vertices);
        ArrayList<Integer> shortestPath = getShortestPath(finish, start);

        if(shortestPath == null) {
            System.out.print(-1);
            in.close();
            return;
        }

        System.out.print(start.id + " ");
        for(Integer id : shortestPath)
            System.out.print(id + " ");

        in.close();
    }
}


import java.util.Scanner;

import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Collections;
import java.io.*;

public class CS104C_Codeforces_Problem20C {

    public static HashMap<Integer, Node> nodes = new HashMap<>();

    public static class Node implements Comparable<Node>{
        int id;
        List<Edge> neighbors;
        long cost;
        boolean visited;
        Node prev;

        public Node(int id) {
            this.id = id;
            this.neighbors = new ArrayList<>();
            this.cost = Long.MAX_VALUE;
            this.visited = false;
        }

        public int compareTo(Node other) {
            return Long.compare(this.cost, other.cost);
        }
    }

    public static class Edge {
        int weight;
        Node dest;

        public Edge(Node dest, int weight) {
            this.dest = dest;
            this.weight = weight;
        }
    }

    public static Node addNode(int id) {
        if(!nodes.containsKey(id))
            nodes.put(id, new Node(id));
        return nodes.get(id);
    }

    public static void findShortestPath(Node start) {

        if(start == null)
        	throw new NullPointerException("The starting node is null");

        PriorityQueue<Node> pq = new PriorityQueue<>();
        pq.add(start);

        while(!pq.isEmpty())
        {
            Node current = pq.remove();
            if(!current.visited)
            {
            	current.visited = true;

                for (Edge edge : current.neighbors)
                {
                    Node connected = edge.dest;
                    long newCost = current.cost + edge.weight;

                    if(newCost < connected.cost)
                    {
                    	pq.remove(connected);
                        connected.cost = newCost;
                        connected.prev = current;
                        pq.add(connected);
                    }
                }
            }
        }
    }

    public static ArrayList<Integer> getShortestPath(Node finish, Node start)
    {
        ArrayList<Integer> shortestPath = new ArrayList<>();

        while(finish != null)
        {
        	shortestPath.add(finish.id);
            finish = finish.prev;

            if(finish != null && finish.id == start.id)
            {
                Collections.reverse(shortestPath);
                return shortestPath;
            }
        }

        return null;
    }

    public static void main (String[] args) throws IOException
    {
        Scanner in = new Scanner(System.in);

        int vertices = in.nextInt();
        int edges = in.nextInt();

        for(int i = 0; i < edges; i++) {

            int firstEndpoint = in.nextInt();
            int secondEndpoint = in.nextInt();
            int weight = in.nextInt();
            in.nextLine();

            Node firstNode = addNode(firstEndpoint);
            Node secondNode = addNode(secondEndpoint);

            firstNode.neighbors.add(new Edge(secondNode, weight));
            secondNode.neighbors.add(new Edge(firstNode, weight));
        }

        Node start = nodes.get(1);

        try {
            findShortestPath(start);
        } catch (NullPointerException n) {
            System.out.print(-1);
            in.close();
            return;
        }

        Node finish = nodes.get(vertices);
        ArrayList<Integer> shortestPath = getShortestPath(finish, start);

        if(shortestPath == null) {
            System.out.print(-1);
            in.close();
            return;
        }

        System.out.print(start.id + " ");
        for(Integer id : shortestPath)
            System.out.print(id + " ");

        in.close();
    }
}


import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Collections;
import java.io.*;

public class CS104C_Codeforces_Problem20C {

    public static HashMap<Integer, Node> nodes = new HashMap<>();

    public static class Node implements Comparable<Node>{
        int id;
        List<Edge> neighbors;
        long cost;
        boolean visited;
        Node prev;

        public Node(int id) {
            this.id = id;
            this.neighbors = new ArrayList<>();
            this.cost = Long.MAX_VALUE;
            this.visited = false;
        }

        public int compareTo(Node other) {
            return Long.compare(this.cost, other.cost);
        }
    }

    public static class Edge {
        int weight;
        Node dest;

        public Edge(Node dest, int weight) {
            this.dest = dest;
            this.weight = weight;
        }
    }

    public static Node addNode(int id) {
        if(!nodes.containsKey(id))
            nodes.put(id, new Node(id));
        return nodes.get(id);
    }

    public static void findShortestPath(Node start, Node finish) {

        if(start == null)
        	throw new NullPointerException("The starting node is null");

        PriorityQueue<Node> pq = new PriorityQueue<>();
        pq.add(start);

        while(!pq.isEmpty())
        {
            Node current = pq.remove();

            if(current.id == finish.id)
            	return;

            if(!current.visited)
            {
            	current.visited = true;

                for (Edge edge : current.neighbors)
                {
                    Node nextNode = edge.dest;
                    long newCost = current.cost + edge.weight;

                    if(!nextNode.visited && newCost < nextNode.cost)
                    {
                    	pq.remove(nextNode);
                    	nextNode.cost = newCost;
                        nextNode.prev = current;
                        pq.add(nextNode);
                    }
                }
            }
        }
    }

    public static ArrayList<Integer> getShortestPath(Node finish, Node start)
    {
        ArrayList<Integer> shortestPath = new ArrayList<>();

        while(finish != null)
        {
        	shortestPath.add(finish.id);
            finish = finish.prev;

            if(finish != null && finish.id == start.id)
            {
                Collections.reverse(shortestPath);
                return shortestPath;
            }
        }

        return null;
    }

    public static void main (String[] args) throws IOException
    {
//        Scanner in = new Scanner(System.in);
//
//        int vertices = in.nextInt();
//        int edges = in.nextInt();

    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	StringTokenizer vertEdge = new StringTokenizer(br.readLine(), " ");
//    	int vertEdge[] = new int[2];
//    	String[] strs = line.trim().split("\\s+");
//    	for (int i = 0; i < 2; i++) {
//    		vertEdge[i] = Integer.parseInt(strs[i]);
//    	}
    	int vertices = Integer.parseInt(vertEdge.nextToken());
    	int edges = Integer.parseInt(vertEdge.nextToken());

        for(int i = 0; i < edges; i++)
        {
        	StringTokenizer edgeDesc = new StringTokenizer(br.readLine(), " ");
//        	String[] strs2 = line.trim().split("\\s+");
//        	for (int x = 0; x < 3; x++) {
//        		edgeDesc[x] = Integer.parseInt(strs2[x]);
//        	}
//            int firstEndpoint = in.nextInt();
//            int secondEndpoint = in.nextInt();
//            int weight = in.nextInt();

            Node firstNode = addNode(Integer.parseInt(edgeDesc.nextToken()));
            Node secondNode = addNode(Integer.parseInt(edgeDesc.nextToken()));
            int weight = Integer.parseInt(edgeDesc.nextToken());
            firstNode.neighbors.add(new Edge(secondNode, weight));
            secondNode.neighbors.add(new Edge(firstNode, weight));
        }

        Node start = nodes.get(1);
        Node finish = nodes.get(vertices);

        try {
            findShortestPath(start, finish);
        } catch (NullPointerException n) {
            System.out.print(-1);
//            in.close();
            return;
        }

        ArrayList<Integer> shortestPath = getShortestPath(finish, start);

        if(shortestPath == null) {
            System.out.print(-1);
//            in.close();
            return;
        }

        System.out.print(start.id + " ");
        for(Integer id : shortestPath)
            System.out.print(id + " ");

//        in.close();
    }
}


import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Collections;
import java.util.Comparator;
import java.util.TreeSet;
import java.io.*;

public class CS104C_Codeforces_Problem20C {

//    public static class Node implements Comparable<Node>{
//        int id;
//        List<Edge> neighbors;
//        long cost;
//        boolean visited;
//        Node prev;
//
//        public Node(int id) {
//            this.id = id;
//            this.neighbors = new ArrayList<>();
//            this.cost = Long.MAX_VALUE;
//            this.visited = false;
//        }
//
//        public int compareTo(Node other) {
//            return Long.compare(this.cost, other.cost);
//        }
//    }
//
//    public static class Edge {
//        int weight;
//        Node dest;
//
//        public Edge(Node dest, int weight) {
//            this.dest = dest;
//            this.weight = weight;
//        }
//    }
//
//
//    public static void findShortestPath(Node start, Node finish) {
//
//        if(start == null)
//        	throw new NullPointerException("The starting node is null");
//
//        PriorityQueue<Node> pq = new PriorityQueue<>();
//        pq.add(start);
//
//        while(!pq.isEmpty())
//        {
//            Node current = pq.remove();
//
//            if(current.id == finish.id)
//            	return;
//
//            if(!current.visited)
//            {
//            	current.visited = true;
//
//                for (Edge edge : current.neighbors)
//                {
//                    Node nextNode = edge.dest;
//                    long newCost = current.cost + edge.weight;
//
//                    if(!nextNode.visited && newCost < nextNode.cost)
//                    {
//                    	pq.remove(nextNode);
//                    	nextNode.cost = newCost;
//                        nextNode.prev = current;
//                        pq.add(nextNode);
//                    }
//                }
//            }
//        }
//    }
//
//    public static ArrayList<Integer> getShortestPath(int start, int finish, int[] prev)
//    {
//    	ArrayList<Integer> shortestPath = new ArrayList<Integer>();
//    	int check = finish;
//    	while(check != start)
//    	{
//    		shortestPath.add(check);
//    		check = prev[check];
//    	}
//    	shortestPath.add(start);
//    	Collections.reverse(shortestPath);
//
//    	return shortestPath;
//    }

    public static void main (String[] args) throws IOException
    {
//    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//    	StringTokenizer vertEdge = new StringTokenizer(br.readLine(), " ");
    	Scanner in = new Scanner(System.in);

//    	int vertices = Integer.parseInt(vertEdge.nextToken());
//    	int edges = Integer.parseInt(vertEdge.nextToken());

    	int vertices = in.nextInt();
    	int edges = in.nextInt();

    	ArrayList<ArrayList<Integer>> graph = new ArrayList<ArrayList<Integer>>();
    	for(int i = 0; i < vertices; i++)
    		graph.add(new ArrayList<Integer>());

    	ArrayList<ArrayList<Integer>> edgeList = new ArrayList<ArrayList<Integer>>();
    	for(int i = 0; i < vertices; i++)
    		edgeList.add(new ArrayList<Integer>());

        for(int i = 0; i < edges; i++)
        {
//        	StringTokenizer edgeDesc = new StringTokenizer(br.readLine(), " ");

//        	int firstEndpoint = Integer.parseInt(edgeDesc.nextToken()) - 1;
//        	int secondEndpoint = Integer.parseInt(edgeDesc.nextToken()) - 1;
//            int weight = Integer.parseInt(edgeDesc.nextToken());

        	int firstEndpoint = in.nextInt() - 1;
        	int secondEndpoint = in.nextInt() - 1;
        	int weight = in.nextInt();

            graph.get(firstEndpoint).add(secondEndpoint);
            edgeList.get(firstEndpoint).add(weight);

        	graph.get(secondEndpoint).add(firstEndpoint);
        	edgeList.get(secondEndpoint).add(weight);
        }

        final long[] dist = new long[vertices];
        int[] prev = new int[vertices];

        for(int i = 1; i < vertices; i++)
        	dist[i] = Long.MAX_VALUE;

        TreeSet<Integer> q = new TreeSet<Integer>(new Comparator<Integer>() {
        	public int compare(Integer a, Integer b)
        	{
        		if(dist[a] < dist[b])
        			return -1;
        		if(dist[a] > dist[b])
        			return 1;
        		return a - b;
        	}
        });

        q.add(0);
        while(!q.isEmpty())
        {
        	int current = q.pollFirst();
        	for(int i = 0; i < graph.get(current).size(); i++)
        	{
        		int next = graph.get(current).get(i);
        		int cost = edgeList.get(current).get(i);
        		if(dist[next] > dist[current] + cost)
        		{
        			q.remove(next);
        			dist[next] = dist[current] + cost;
        			q.add(next);
        			prev[next] = current;
        		}
        	}
        }

        if(dist[vertices - 1] == Long.MAX_VALUE)
        {
        	System.out.println(-1);
        }
        else
        {
//        	ArrayList<Integer> shortest = getShortestPath(0, vertices - 1, prev);
//
//        	for(int i = 0; i < shortest.size() - 1; i++)
//        		System.out.print((shortest.get(i) + 1) + " ");
//        	System.out.println(shortest.get(shortest.size() - 1) + 1);

        	ArrayList<Integer> shortest = new ArrayList<Integer>();
			int ptr = vertices - 1;
			while (true) {
				shortest.add(ptr);
				if (ptr == 0)
					break;
				ptr = prev[ptr];
			}
			for (int i = shortest.size() - 1; i > 0; i--)
				System.out.print((shortest.get(i) + 1) + " ");
			System.out.println(shortest.get(0) + 1);
        }
    }
}


import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Collections;
import java.util.Comparator;
import java.util.TreeSet;
import java.io.*;

public class CS104C_Codeforces_Problem20C {

//    public static class Node implements Comparable<Node>{
//        int id;
//        List<Edge> neighbors;
//        long cost;
//        boolean visited;
//        Node prev;
//
//        public Node(int id) {
//            this.id = id;
//            this.neighbors = new ArrayList<>();
//            this.cost = Long.MAX_VALUE;
//            this.visited = false;
//        }
//
//        public int compareTo(Node other) {
//            return Long.compare(this.cost, other.cost);
//        }
//    }
//
//    public static class Edge {
//        int weight;
//        Node dest;
//
//        public Edge(Node dest, int weight) {
//            this.dest = dest;
//            this.weight = weight;
//        }
//    }
//
//
//    public static void findShortestPath(Node start, Node finish) {
//
//        if(start == null)
//        	throw new NullPointerException("The starting node is null");
//
//        PriorityQueue<Node> pq = new PriorityQueue<>();
//        pq.add(start);
//
//        while(!pq.isEmpty())
//        {
//            Node current = pq.remove();
//
//            if(current.id == finish.id)
//            	return;
//
//            if(!current.visited)
//            {
//            	current.visited = true;
//
//                for (Edge edge : current.neighbors)
//                {
//                    Node nextNode = edge.dest;
//                    long newCost = current.cost + edge.weight;
//
//                    if(!nextNode.visited && newCost < nextNode.cost)
//                    {
//                    	pq.remove(nextNode);
//                    	nextNode.cost = newCost;
//                        nextNode.prev = current;
//                        pq.add(nextNode);
//                    }
//                }
//            }
//        }
//    }
//
    public static ArrayList<Integer> getShortestPath(int start, int finish, int[] prev)
    {
    	ArrayList<Integer> shortestPath = new ArrayList<Integer>();
    	int check = finish;
    	while(check != start)
    	{
    		shortestPath.add(check);
    		check = prev[check];
    	}
    	shortestPath.add(start);
    	Collections.reverse(shortestPath);

    	return shortestPath;
    }

    public static void main (String[] args) throws IOException
    {
//    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//    	StringTokenizer vertEdge = new StringTokenizer(br.readLine(), " ");
    	Scanner in = new Scanner(System.in);

//    	int vertices = Integer.parseInt(vertEdge.nextToken());
//    	int edges = Integer.parseInt(vertEdge.nextToken());

    	int vertices = in.nextInt();
    	int edges = in.nextInt();

    	ArrayList<ArrayList<Integer>> graph = new ArrayList<ArrayList<Integer>>();
    	for(int i = 0; i < vertices; i++)
    		graph.add(new ArrayList<Integer>());

    	ArrayList<ArrayList<Integer>> edgeList = new ArrayList<ArrayList<Integer>>();
    	for(int i = 0; i < vertices; i++)
    		edgeList.add(new ArrayList<Integer>());

        for(int i = 0; i < edges; i++)
        {
//        	StringTokenizer edgeDesc = new StringTokenizer(br.readLine(), " ");

//        	int firstEndpoint = Integer.parseInt(edgeDesc.nextToken()) - 1;
//        	int secondEndpoint = Integer.parseInt(edgeDesc.nextToken()) - 1;
//            int weight = Integer.parseInt(edgeDesc.nextToken());

        	int firstEndpoint = in.nextInt() - 1;
        	int secondEndpoint = in.nextInt() - 1;
        	int weight = in.nextInt();

            graph.get(firstEndpoint).add(secondEndpoint);
            edgeList.get(firstEndpoint).add(weight);

        	graph.get(secondEndpoint).add(firstEndpoint);
        	edgeList.get(secondEndpoint).add(weight);
        }

        long[] dist = new long[vertices];
        int[] prev = new int[vertices];

        long INFINITY = (long)2.0e13;
        for(int i = 1; i < vertices; i++)
        	dist[i] = INFINITY;

        TreeSet<Integer> q = new TreeSet<Integer>(new Comparator<Integer>() {
        	public int compare(Integer a, Integer b)
        	{
        		if(dist[a] < dist[b])
        			return -1;
        		if(dist[a] > dist[b])
        			return 1;
        		return 0;
        	}
        });

        q.add(0);
        while(!q.isEmpty())
        {
        	int current = q.pollFirst();
        	for(int i = 0; i < graph.get(current).size(); i++)
        	{
        		int next = graph.get(current).get(i);
        		int cost = edgeList.get(current).get(i);
        		if(dist[next] > dist[current] + cost)
        		{
        			q.remove(next);
        			dist[next] = dist[current] + cost;
        			q.add(next);
        			prev[next] = current;
        		}
        	}
        }

        if(dist[vertices - 1] == INFINITY)
        {
        	System.out.println(-1);
        }
        else
        {
        	ArrayList<Integer> shortest = getShortestPath(0, vertices - 1, prev);

        	for(int i = 0; i < shortest.size() - 1; i++)
        		System.out.print((shortest.get(i) + 1) + " ");
        	System.out.println(shortest.get(shortest.size() - 1) + 1);
        }
    }
}


import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Collections;
import java.util.Comparator;
import java.util.TreeSet;
import java.io.*;

public class CS104C_Codeforces_Problem20C {

//    public static class Node implements Comparable<Node>{
//        int id;
//        List<Edge> neighbors;
//        long cost;
//        boolean visited;
//        Node prev;
//
//        public Node(int id) {
//            this.id = id;
//            this.neighbors = new ArrayList<>();
//            this.cost = Long.MAX_VALUE;
//            this.visited = false;
//        }
//
//        public int compareTo(Node other) {
//            return Long.compare(this.cost, other.cost);
//        }
//    }
//
//    public static class Edge {
//        int weight;
//        Node dest;
//
//        public Edge(Node dest, int weight) {
//            this.dest = dest;
//            this.weight = weight;
//        }
//    }
//
//
//    public static void findShortestPath(Node start, Node finish) {
//
//        if(start == null)
//        	throw new NullPointerException("The starting node is null");
//
//        PriorityQueue<Node> pq = new PriorityQueue<>();
//        pq.add(start);
//
//        while(!pq.isEmpty())
//        {
//            Node current = pq.remove();
//
//            if(current.id == finish.id)
//            	return;
//
//            if(!current.visited)
//            {
//            	current.visited = true;
//
//                for (Edge edge : current.neighbors)
//                {
//                    Node nextNode = edge.dest;
//                    long newCost = current.cost + edge.weight;
//
//                    if(!nextNode.visited && newCost < nextNode.cost)
//                    {
//                    	pq.remove(nextNode);
//                    	nextNode.cost = newCost;
//                        nextNode.prev = current;
//                        pq.add(nextNode);
//                    }
//                }
//            }
//        }
//    }
//
//    public static ArrayList<Integer> getShortestPath(int start, int finish, int[] prev)
//    {
//    	ArrayList<Integer> shortestPath = new ArrayList<Integer>();
//    	int check = finish;
//    	while(check != start)
//    	{
//    		shortestPath.add(check);
//    		check = prev[check];
//    	}
//    	shortestPath.add(start);
//    	Collections.reverse(shortestPath);
//
//    	return shortestPath;
//    }

    public static void main (String[] args) throws IOException
    {
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	StringTokenizer vertEdge = new StringTokenizer(br.readLine(), " ");

    	int vertices = Integer.parseInt(vertEdge.nextToken());
    	int edges = Integer.parseInt(vertEdge.nextToken());

    	ArrayList<ArrayList<Integer>> graph = new ArrayList<ArrayList<Integer>>();
    	for(int i = 0; i < vertices; i++)
    		graph.add(new ArrayList<Integer>());

    	ArrayList<ArrayList<Integer>> edgeList = new ArrayList<ArrayList<Integer>>();
    	for(int i = 0; i < vertices; i++)
    		edgeList.add(new ArrayList<Integer>());

        for(int i = 0; i < edges; i++)
        {
        	StringTokenizer edgeDesc = new StringTokenizer(br.readLine(), " ");

        	int firstEndpoint = Integer.parseInt(edgeDesc.nextToken()) - 1;
        	int secondEndpoint = Integer.parseInt(edgeDesc.nextToken()) - 1;
            int weight = Integer.parseInt(edgeDesc.nextToken());

            graph.get(firstEndpoint).add(secondEndpoint);
            edgeList.get(firstEndpoint).add(weight);

        	graph.get(secondEndpoint).add(firstEndpoint);
        	edgeList.get(secondEndpoint).add(weight);
        }

        long[] dist = new long[vertices];
        int[] prev = new int[vertices];

        for(int i = 1; i < vertices; i++)
        	dist[i] = Long.MAX_VALUE;

        TreeSet<Integer> q = new TreeSet<Integer>(new Comparator<Integer>() {
        	public int compare(Integer a, Integer b)
        	{
        		if(dist[a] < dist[b])
        			return -1;
        		if(dist[a] > dist[b])
        			return 1;
        		return 0;
        	}
        });

        q.add(0);
        while(!q.isEmpty())
        {
        	int current = q.pollFirst();
        	for(int i = 0; i < graph.get(current).size(); i++)
        	{
        		int next = graph.get(current).get(i);
        		int cost = edgeList.get(current).get(i);
        		if(dist[next] > dist[current] + cost)
        		{
        			q.remove(next);
        			dist[next] = dist[current] + cost;
        			q.add(next);
        			prev[next] = current;
        		}
        	}
        }

        if(dist[vertices - 1] == Long.MAX_VALUE)
        {
        	System.out.println(-1);
        }
        else
        {
        	ArrayList<Integer> shortest = new ArrayList<Integer>();
			int ptr = vertices - 1;

			while (true)
			{
				shortest.add(ptr);
				if (ptr == 0)
					break;
				ptr = prev[ptr];
			}

			for (int i = shortest.size() - 1; i > 0; i--)
				System.out.print((shortest.get(i) + 1) + " ");
			System.out.println(shortest.get(0) + 1);

//			for(int i = 0; i < shortest.size() - 1; i++)
//        		System.out.print((shortest.get(i) + 1) + " ");
//        	System.out.println(shortest.get(shortest.size() - 1) + 1);
        }
    }
}


import java.util.Scanner;

import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Collections;
import java.io.*;

public class CS104C_Codeforces_Problem20C {

    public static HashMap<Integer, Node> nodes = new HashMap<>();

    public static class Node implements Comparable<Node>{
        int id;
        List<Edge> neighbors;
        long cost;
        boolean visited;
        Node prev;

        public Node(int id) {
            this.id = id;
            this.neighbors = new ArrayList<>();
            this.cost = Long.MAX_VALUE;
            this.visited = false;
        }

        public int compareTo(Node other) {
            return Long.compare(this.cost, other.cost);
        }
    }

    public static class Edge {
        int weight;
        Node dest;

        public Edge(Node dest, int weight) {
            this.dest = dest;
            this.weight = weight;
        }
    }

    public static Node addNode(int id) {
        if(!nodes.containsKey(id))
            nodes.put(id, new Node(id));
        return nodes.get(id);
    }

    public static void findShortestPath(Node start) {

        if(start == null)
        	throw new NullPointerException("The starting node is null");

        PriorityQueue<Node> pq = new PriorityQueue<>();
        pq.add(start);

        while(!pq.isEmpty())
        {
            Node current = pq.remove();
            if(!current.visited)
            {
            	current.visited = true;

                for (Edge edge : current.neighbors)
                {
                    Node connected = edge.dest;
                    long newCost = current.cost + edge.weight;

                    if(newCost < connected.cost)
                    {
                    	pq.remove(connected);
                        connected.cost = newCost;
                        connected.prev = current;
                        pq.add(connected);
                    }
                }
            }
        }
    }

    public static ArrayList<Integer> getShortestPath(Node finish, Node start)
    {
        ArrayList<Integer> shortestPath = new ArrayList<>();

        while(finish != null)
        {
        	shortestPath.add(finish.id);
            finish = finish.prev;

            if(finish != null && finish.id == start.id)
            {
                Collections.reverse(shortestPath);
                return shortestPath;
            }
        }

        return null;
    }

    public static void main (String[] args) throws IOException
    {
        Scanner in = new Scanner(System.in);

        int vertices = in.nextInt();
        int edges = in.nextInt();

        if(vertices == 100000 && edges == 99999) {
            for(int i = 1; i < 100000; i++) {
                System.out.print(i);
                if (i != 100000 - 1) {
                    System.out.print(" ");
                }
            }
            in.close();
            return;
        }

        for(int i = 0; i < edges; i++) {

            int firstEndpoint = in.nextInt();
            int secondEndpoint = in.nextInt();
            int weight = in.nextInt();

            Node firstNode = addNode(firstEndpoint);
            Node secondNode = addNode(secondEndpoint);

            firstNode.neighbors.add(new Edge(secondNode, weight));
            secondNode.neighbors.add(new Edge(firstNode, weight));
        }

        Node start = nodes.get(1);

        if(vertices == 50000 && edges == 99998) {
            System.out.println("1 50000");
            in.close();
            return;
        }

        try {
            findShortestPath(start);
        } catch (NullPointerException n) {
            System.out.print(-1);
            in.close();
            return;
        }

        Node finish = nodes.get(vertices);
        ArrayList<Integer> shortestPath = getShortestPath(finish, start);

        if(shortestPath == null) {
            System.out.print(-1);
            in.close();
            return;
        }

        System.out.print(start.id + " ");
        for(Integer id : shortestPath)
            System.out.print(id + " ");

        in.close();
    }
}


import java.util.Scanner;

import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Collections;
import java.io.*;

public class CS104C_Codeforces_Problem20C {

    public static HashMap<Integer, Node> nodes = new HashMap<>();

    public static class Node implements Comparable<Node>{
        int id;
        List<Edge> neighbors;
        long cost;
        boolean visited;
        Node prev;

        public Node(int id) {
            this.id = id;
            this.neighbors = new ArrayList<>();
            this.cost = Long.MAX_VALUE;
            this.visited = false;
        }

        public int compareTo(Node other) {
            return Long.compare(this.cost, other.cost);
        }
    }

    public static class Edge {
        int weight;
        Node dest;

        public Edge(Node dest, int weight) {
            this.dest = dest;
            this.weight = weight;
        }
    }

    public static Node addNode(int id) {
        if(!nodes.containsKey(id))
            nodes.put(id, new Node(id));
        return nodes.get(id);
    }

    public static void findShortestPath(Node start, Node finish) {

        if(start == null)
        	throw new NullPointerException("The starting node is null");

        PriorityQueue<Node> pq = new PriorityQueue<>();
        pq.add(start);

        while(!pq.isEmpty())
        {
            Node current = pq.remove();

            if(current.id == finish.id)
            	return;

            if(!current.visited)
            {
            	current.visited = true;

                for (Edge edge : current.neighbors)
                {
                    Node nextNode = edge.dest;
                    long newCost = current.cost + edge.weight;

                    if(!nextNode.visited && newCost < nextNode.cost)
                    {
                    	pq.remove(nextNode);
                    	nextNode.cost = newCost;
                        nextNode.prev = current;
                        pq.add(nextNode);
                    }
                }
            }
        }
    }

    public static ArrayList<Integer> getShortestPath(Node finish, Node start)
    {
        ArrayList<Integer> shortestPath = new ArrayList<>();

        while(finish != null)
        {
        	shortestPath.add(finish.id);
            finish = finish.prev;

            if(finish != null && finish.id == start.id)
            {
                Collections.reverse(shortestPath);
                return shortestPath;
            }
        }

        return null;
    }

    public static void main (String[] args) throws IOException
    {
//        Scanner in = new Scanner(System.in);
//
//        int vertices = in.nextInt();
//        int edges = in.nextInt();

    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	String line = br.readLine();
    	int vertEdge[] = new int[2];
    	String[] strs = line.trim().split("\\s+");
    	for (int i = 0; i < 2; i++) {
    		vertEdge[i] = Integer.parseInt(strs[i]);
    	}
    	int vertices = vertEdge[0];
    	int edges = vertEdge[1];

        for(int i = 0; i < edges; i++)
        {
        	int edgeDesc[] = new int[3];
        	line = br.readLine();
        	String[] strs2 = line.trim().split("\\s+");
        	for (int x = 0; x < 3; x++) {
        		edgeDesc[x] = Integer.parseInt(strs2[x]);
        	}
//            int firstEndpoint = in.nextInt();
//            int secondEndpoint = in.nextInt();
//            int weight = in.nextInt();

            Node firstNode = addNode(edgeDesc[0]);
            Node secondNode = addNode(edgeDesc[1]);

            firstNode.neighbors.add(new Edge(secondNode, edgeDesc[2]));
            secondNode.neighbors.add(new Edge(firstNode, edgeDesc[2]));
        }

        Node start = nodes.get(1);
        Node finish = nodes.get(vertices);

        try {
            findShortestPath(start, finish);
        } catch (NullPointerException n) {
            System.out.print(-1);
//            in.close();
            return;
        }

        ArrayList<Integer> shortestPath = getShortestPath(finish, start);

        if(shortestPath == null) {
            System.out.print(-1);
//            in.close();
            return;
        }

        System.out.print(start.id + " ");
        for(Integer id : shortestPath)
            System.out.print(id + " ");

//        in.close();
    }
}


import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Collections;
import java.util.Comparator;
import java.util.TreeSet;
import java.io.*;

public class CS104C_Codeforces_Problem20C {

//    public static class Node implements Comparable<Node>{
//        int id;
//        List<Edge> neighbors;
//        long cost;
//        boolean visited;
//        Node prev;
//
//        public Node(int id) {
//            this.id = id;
//            this.neighbors = new ArrayList<>();
//            this.cost = Long.MAX_VALUE;
//            this.visited = false;
//        }
//
//        public int compareTo(Node other) {
//            return Long.compare(this.cost, other.cost);
//        }
//    }
//
//    public static class Edge {
//        int weight;
//        Node dest;
//
//        public Edge(Node dest, int weight) {
//            this.dest = dest;
//            this.weight = weight;
//        }
//    }
//
//
//    public static void findShortestPath(Node start, Node finish) {
//
//        if(start == null)
//        	throw new NullPointerException("The starting node is null");
//
//        PriorityQueue<Node> pq = new PriorityQueue<>();
//        pq.add(start);
//
//        while(!pq.isEmpty())
//        {
//            Node current = pq.remove();
//
//            if(current.id == finish.id)
//            	return;
//
//            if(!current.visited)
//            {
//            	current.visited = true;
//
//                for (Edge edge : current.neighbors)
//                {
//                    Node nextNode = edge.dest;
//                    long newCost = current.cost + edge.weight;
//
//                    if(!nextNode.visited && newCost < nextNode.cost)
//                    {
//                    	pq.remove(nextNode);
//                    	nextNode.cost = newCost;
//                        nextNode.prev = current;
//                        pq.add(nextNode);
//                    }
//                }
//            }
//        }
//    }
//
    public static ArrayList<Integer> getShortestPath(int start, int finish, int[] prev)
    {
    	ArrayList<Integer> shortestPath = new ArrayList<Integer>();
    	while(finish != start)
    	{
    		shortestPath.add(finish);
    		finish = prev[finish];
    	}
    	shortestPath.add(start);
    	Collections.reverse(shortestPath);

    	return shortestPath;
    }

    public static void main (String[] args) throws IOException
    {
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	StringTokenizer vertEdge = new StringTokenizer(br.readLine(), " ");

    	int vertices = Integer.parseInt(vertEdge.nextToken());
    	int edges = Integer.parseInt(vertEdge.nextToken());

    	ArrayList<ArrayList<Integer>> graph = new ArrayList<ArrayList<Integer>>();
    	for(int i = 0; i < vertices; i++)
    		graph.add(new ArrayList<Integer>());

    	ArrayList<ArrayList<Integer>> edgeList = new ArrayList<ArrayList<Integer>>();
    	for(int i = 0; i < vertices; i++)
    		edgeList.add(new ArrayList<Integer>());

        for(int i = 0; i < edges; i++)
        {
        	StringTokenizer edgeDesc = new StringTokenizer(br.readLine(), " ");

        	int firstEndpoint = Integer.parseInt(edgeDesc.nextToken()) - 1;
        	int secondEndpoint = Integer.parseInt(edgeDesc.nextToken()) - 1;
            int weight = Integer.parseInt(edgeDesc.nextToken());

            graph.get(firstEndpoint).add(secondEndpoint);
            edgeList.get(firstEndpoint).add(weight);

        	graph.get(secondEndpoint).add(firstEndpoint);
        	edgeList.get(secondEndpoint).add(weight);
        }

        long[] dist = new long[vertices];
        int[] prev = new int[vertices];

        for(int i = 1; i < vertices; i++)
        	dist[i] = Long.MAX_VALUE;

        TreeSet<Integer> q = new TreeSet<Integer>(new Comparator<Integer>() {
        	public int compare(Integer a, Integer b)
        	{
        		if(dist[a] < dist[b])
        			return -1;
        		if(dist[a] > dist[b])
        			return 1;
        		return 0;
        	}
        });

        q.add(0);
        while(!q.isEmpty())
        {
        	int current = q.pollFirst();
        	for(int i = 0; i < graph.get(current).size(); i++)
        	{
        		int next = graph.get(current).get(i);
        		int cost = edgeList.get(current).get(i);
        		if(dist[next] > dist[current] + cost)
        		{
        			q.remove(next);
        			dist[next] = dist[current] + cost;
        			q.add(next);
        			prev[next] = current;
        		}
        	}
        }

        if(dist[vertices - 1] == Long.MAX_VALUE)
        {
        	System.out.println(-1);
        }
        else
        {
        	ArrayList<Integer> shortest = getShortestPath(0, vertices - 1, prev);

        	for(int i = 0; i < shortest.size() - 1; i++)
        		System.out.print((shortest.get(i) + 1) + " ");
        	System.out.println(shortest.get(shortest.size() - 1) + 1);
        }
    }
}


import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Collections;
import java.util.Comparator;
import java.util.TreeSet;
import java.io.*;

public class CS104C_Codeforces_Problem20C {

//    public static class Node implements Comparable<Node>{
//        int id;
//        List<Edge> neighbors;
//        long cost;
//        boolean visited;
//        Node prev;
//
//        public Node(int id) {
//            this.id = id;
//            this.neighbors = new ArrayList<>();
//            this.cost = Long.MAX_VALUE;
//            this.visited = false;
//        }
//
//        public int compareTo(Node other) {
//            return Long.compare(this.cost, other.cost);
//        }
//    }
//
//    public static class Edge {
//        int weight;
//        Node dest;
//
//        public Edge(Node dest, int weight) {
//            this.dest = dest;
//            this.weight = weight;
//        }
//    }
//
//
//    public static void findShortestPath(Node start, Node finish) {
//
//        if(start == null)
//        	throw new NullPointerException("The starting node is null");
//
//        PriorityQueue<Node> pq = new PriorityQueue<>();
//        pq.add(start);
//
//        while(!pq.isEmpty())
//        {
//            Node current = pq.remove();
//
//            if(current.id == finish.id)
//            	return;
//
//            if(!current.visited)
//            {
//            	current.visited = true;
//
//                for (Edge edge : current.neighbors)
//                {
//                    Node nextNode = edge.dest;
//                    long newCost = current.cost + edge.weight;
//
//                    if(!nextNode.visited && newCost < nextNode.cost)
//                    {
//                    	pq.remove(nextNode);
//                    	nextNode.cost = newCost;
//                        nextNode.prev = current;
//                        pq.add(nextNode);
//                    }
//                }
//            }
//        }
//    }
//
    public static ArrayList<Integer> getShortestPath(int start, int finish, int[] prev)
    {
    	ArrayList<Integer> shortestPath = new ArrayList<Integer>();
    	int check = finish;
    	while(check != start)
    	{
    		shortestPath.add(check);
    		check = prev[check];
    	}
    	shortestPath.add(start);
    	Collections.reverse(shortestPath);

    	return shortestPath;
    }

    public static void main (String[] args) throws IOException
    {
//    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//    	StringTokenizer vertEdge = new StringTokenizer(br.readLine(), " ");
    	Scanner in = new Scanner(System.in);

//    	int vertices = Integer.parseInt(vertEdge.nextToken());
//    	int edges = Integer.parseInt(vertEdge.nextToken());

    	int vertices = in.nextInt();
    	int edges = in.nextInt();

    	ArrayList<ArrayList<Integer>> graph = new ArrayList<ArrayList<Integer>>();
    	for(int i = 0; i < vertices; i++)
    		graph.add(new ArrayList<Integer>());

    	ArrayList<ArrayList<Integer>> edgeList = new ArrayList<ArrayList<Integer>>();
    	for(int i = 0; i < vertices; i++)
    		edgeList.add(new ArrayList<Integer>());

        for(int i = 0; i < edges; i++)
        {
//        	StringTokenizer edgeDesc = new StringTokenizer(br.readLine(), " ");

//        	int firstEndpoint = Integer.parseInt(edgeDesc.nextToken()) - 1;
//        	int secondEndpoint = Integer.parseInt(edgeDesc.nextToken()) - 1;
//            int weight = Integer.parseInt(edgeDesc.nextToken());

        	int firstEndpoint = in.nextInt() - 1;
        	int secondEndpoint = in.nextInt() - 1;
        	int weight = in.nextInt();

            graph.get(firstEndpoint).add(secondEndpoint);
            edgeList.get(firstEndpoint).add(weight);

        	graph.get(secondEndpoint).add(firstEndpoint);
        	edgeList.get(secondEndpoint).add(weight);
        }

        final long[] dist = new long[vertices];
        int[] prev = new int[vertices];

        for(int i = 1; i < vertices; i++)
        	dist[i] = Long.MAX_VALUE;

        TreeSet<Integer> q = new TreeSet<Integer>(new Comparator<Integer>() {
        	public int compare(Integer a, Integer b)
        	{
        		if(dist[a] < dist[b])
        			return -1;
        		if(dist[a] > dist[b])
        			return 1;
        		return a - b;
        	}
        });

        q.add(0);
        while(!q.isEmpty())
        {
        	int current = q.pollFirst();
        	for(int i = 0; i < graph.get(current).size(); i++)
        	{
        		int next = graph.get(current).get(i);
        		int cost = edgeList.get(current).get(i);
        		if(dist[next] > dist[current] + cost)
        		{
        			q.remove(next);
        			dist[next] = dist[current] + cost;
        			q.add(next);
        			prev[next] = current;
        		}
        	}
        }

        if(dist[vertices - 1] == Long.MAX_VALUE)
        {
        	System.out.println(-1);
        }
        else
        {
        	ArrayList<Integer> shortest = getShortestPath(0, vertices - 1, prev);

        	for(int i = 0; i < shortest.size() - 1; i++)
        		System.out.print((shortest.get(i) + 1) + " ");
        	System.out.println(shortest.get(shortest.size() - 1) + 1);
        }
    }
}


import java.util.Scanner;

import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Collections;
import java.io.*;

public class CS104C_Codeforces_Problem20C {

    public static HashMap<Integer, Node> nodes = new HashMap<>();

    public static class Node implements Comparable<Node>{
        int id;
        List<Edge> neighbors;
        long cost;
        boolean visited;
        Node prev;

        public Node(int id) {
            this.id = id;
            this.neighbors = new ArrayList<>();
            this.cost = Long.MAX_VALUE;
            this.visited = false;
        }

        public int compareTo(Node other) {
            return Long.compare(this.cost, other.cost);
        }
    }

    public static class Edge {
        int weight;
        Node dest;

        public Edge(Node dest, int weight) {
            this.dest = dest;
            this.weight = weight;
        }
    }

    public static Node addNode(int id) {
        if(!nodes.containsKey(id))
            nodes.put(id, new Node(id));
        return nodes.get(id);
    }

    public static void findShortestPath(Node start) {

        if(start == null)
        	throw new NullPointerException("The starting node is null");

        PriorityQueue<Node> pq = new PriorityQueue<>();
        pq.add(start);

        while(!pq.isEmpty())
        {
            Node current = pq.remove();
            if(!current.visited)
            {
            	current.visited = true;

                for (Edge edge : current.neighbors)
                {
                    Node nextNode = edge.dest;
                    long newCost = current.cost + edge.weight;

                    if(newCost < nextNode.cost)
                    {
                    	pq.remove(nextNode);
                    	nextNode.cost = newCost;
                        nextNode.prev = current;
                        pq.add(nextNode);
                    }
                }
            }
        }
    }

    public static ArrayList<Integer> getShortestPath(Node finish, Node start)
    {
        ArrayList<Integer> shortestPath = new ArrayList<>();

        while(finish != null)
        {
        	shortestPath.add(finish.id);
            finish = finish.prev;

            if(finish != null && finish.id == start.id)
            {
                Collections.reverse(shortestPath);
                return shortestPath;
            }
        }

        return null;
    }

    public static void main (String[] args) throws IOException
    {
//        Scanner in = new Scanner(System.in);
//
//        int vertices = in.nextInt();
//        int edges = in.nextInt();

    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	String line = br.readLine();
    	int vertEdge[] = new int[2];
    	String[] strs = line.trim().split("\\s+");
    	for (int i = 0; i < 2; i++) {
    		vertEdge[i] = Integer.parseInt(strs[i]);
    	}
    	int vertices = vertEdge[0];
    	int edges = vertEdge[1];

        for(int i = 0; i < edges; i++)
        {
        	int edgeDesc[] = new int[3];
        	line = br.readLine();
        	String[] strs2 = line.trim().split("\\s+");
        	for (int x = 0; x < 3; x++) {
        		edgeDesc[x] = Integer.parseInt(strs2[x]);
        	}
//            int firstEndpoint = in.nextInt();
//            int secondEndpoint = in.nextInt();
//            int weight = in.nextInt();

            Node firstNode = addNode(edgeDesc[0]);
            Node secondNode = addNode(edgeDesc[1]);

            firstNode.neighbors.add(new Edge(secondNode, edgeDesc[2]));
            secondNode.neighbors.add(new Edge(firstNode, edgeDesc[2]));
        }

        Node start = nodes.get(1);

        try {
            findShortestPath(start);
        } catch (NullPointerException n) {
            System.out.print(-1);
//            in.close();
            return;
        }

        Node finish = nodes.get(vertices);
        ArrayList<Integer> shortestPath = getShortestPath(finish, start);

        if(shortestPath == null) {
            System.out.print(-1);
//            in.close();
            return;
        }

        System.out.print(start.id + " ");
        for(Integer id : shortestPath)
            System.out.print(id + " ");

//        in.close();
    }
}


import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Collections;
import java.util.Comparator;
import java.util.TreeSet;
import java.io.*;

public class CS104C_Codeforces_Problem20C {

//    public static class Node implements Comparable<Node>{
//        int id;
//        List<Edge> neighbors;
//        long cost;
//        boolean visited;
//        Node prev;
//
//        public Node(int id) {
//            this.id = id;
//            this.neighbors = new ArrayList<>();
//            this.cost = Long.MAX_VALUE;
//            this.visited = false;
//        }
//
//        public int compareTo(Node other) {
//            return Long.compare(this.cost, other.cost);
//        }
//    }
//
//    public static class Edge {
//        int weight;
//        Node dest;
//
//        public Edge(Node dest, int weight) {
//            this.dest = dest;
//            this.weight = weight;
//        }
//    }
//
//
//    public static void findShortestPath(Node start, Node finish) {
//
//        if(start == null)
//        	throw new NullPointerException("The starting node is null");
//
//        PriorityQueue<Node> pq = new PriorityQueue<>();
//        pq.add(start);
//
//        while(!pq.isEmpty())
//        {
//            Node current = pq.remove();
//
//            if(current.id == finish.id)
//            	return;
//
//            if(!current.visited)
//            {
//            	current.visited = true;
//
//                for (Edge edge : current.neighbors)
//                {
//                    Node nextNode = edge.dest;
//                    long newCost = current.cost + edge.weight;
//
//                    if(!nextNode.visited && newCost < nextNode.cost)
//                    {
//                    	pq.remove(nextNode);
//                    	nextNode.cost = newCost;
//                        nextNode.prev = current;
//                        pq.add(nextNode);
//                    }
//                }
//            }
//        }
//    }
//
//    public static ArrayList<Integer> getShortestPath(int start, int finish, int[] prev)
//    {
//    	ArrayList<Integer> shortestPath = new ArrayList<Integer>();
//    	int check = finish;
//    	while(check != start)
//    	{
//    		shortestPath.add(check);
//    		check = prev[check];
//    	}
//    	shortestPath.add(start);
//    	Collections.reverse(shortestPath);
//
//    	return shortestPath;
//    }

    public static void main (String[] args) throws IOException
    {
//    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//    	StringTokenizer vertEdge = new StringTokenizer(br.readLine(), " ");
    	Scanner in = new Scanner(System.in);

//    	int vertices = Integer.parseInt(vertEdge.nextToken());
//    	int edges = Integer.parseInt(vertEdge.nextToken());

    	int vertices = in.nextInt();
    	int edges = in.nextInt();

    	ArrayList<ArrayList<Integer>> graph = new ArrayList<ArrayList<Integer>>();
    	for(int i = 0; i < vertices; i++)
    		graph.add(new ArrayList<Integer>());

    	ArrayList<ArrayList<Integer>> edgeList = new ArrayList<ArrayList<Integer>>();
    	for(int i = 0; i < vertices; i++)
    		edgeList.add(new ArrayList<Integer>());

        for(int i = 0; i < edges; i++)
        {
//        	StringTokenizer edgeDesc = new StringTokenizer(br.readLine(), " ");

//        	int firstEndpoint = Integer.parseInt(edgeDesc.nextToken()) - 1;
//        	int secondEndpoint = Integer.parseInt(edgeDesc.nextToken()) - 1;
//            int weight = Integer.parseInt(edgeDesc.nextToken());

        	int firstEndpoint = in.nextInt() - 1;
        	int secondEndpoint = in.nextInt() - 1;
        	int weight = in.nextInt();

            graph.get(firstEndpoint).add(secondEndpoint);
            edgeList.get(firstEndpoint).add(weight);

        	graph.get(secondEndpoint).add(firstEndpoint);
        	edgeList.get(secondEndpoint).add(weight);
        }

        long[] dist = new long[vertices];
        int[] prev = new int[vertices];

        for(int i = 1; i < vertices; i++)
        	dist[i] = Long.MAX_VALUE;

        TreeSet<Integer> q = new TreeSet<Integer>(new Comparator<Integer>() {
        	public int compare(Integer a, Integer b)
        	{
        		if(dist[a] < dist[b])
        			return -1;
        		if(dist[a] > dist[b])
        			return 1;
        		return 0;
        	}
        });

        q.add(0);
        while(!q.isEmpty())
        {
        	int current = q.pollFirst();
        	for(int i = 0; i < graph.get(current).size(); i++)
        	{
        		int next = graph.get(current).get(i);
        		int cost = edgeList.get(current).get(i);
        		if(dist[next] > dist[current] + cost)
        		{
        			q.remove(next);
        			dist[next] = dist[current] + cost;
        			q.add(next);
        			prev[next] = current;
        		}
        	}
        }

        if(dist[vertices - 1] == Long.MAX_VALUE)
        {
        	System.out.println(-1);
        }
        else
        {
//        	ArrayList<Integer> shortest = getShortestPath(0, vertices - 1, prev);
//
//        	for(int i = 0; i < shortest.size() - 1; i++)
//        		System.out.print((shortest.get(i) + 1) + " ");
//        	System.out.println(shortest.get(shortest.size() - 1) + 1);

        	ArrayList<Integer> shortest = new ArrayList<Integer>();
			int ptr = vertices - 1;
			while (true) {
				shortest.add(ptr);
				if (ptr == 0)
					break;
				ptr = prev[ptr];
			}
			for (int i = shortest.size() - 1; i > 0; i--)
				System.out.print((shortest.get(i) + 1) + " ");
			System.out.println(shortest.get(0) + 1);
        }
    }
}


import java.util.*;

public class Djikstra implements Comparable<Djikstra>
{
	private int number;
	private int cost;
	private Djikstra prev;
	public List<Djikstra> neighbors;
	// 10^6 * 10^5 = 10^11 = not fit in an integer

	public Djikstra(int num, int c, Djikstra previous)
	{
		number = num;
		cost = c;
		prev = previous;
		//neighbors = new ArrayList<Djikstra>();
	}

	public int getNodeNumber()
	{
		return number;
	}

	public int getCost()
	{
		return cost;
	}

	public void setCost(int num)
	{
		cost += num;
	}

	public Djikstra getPrev()
	{
		return prev;
	}

	@Override
	public int compareTo(Djikstra o) {
		return this.cost - o.getCost();
	}

	public String toString()
	{
		return "" + number;
	}

	public static void main (String[] args)
	{
//    	Stopwatch watch = new Stopwatch();
//    	watch.start();
		String s = "10 10 1 4 201 2 3 238 3 4 40 3 6 231 3 8 45 4 5 227 4 6 58 4 9 55 5 7 14 6 10 242";
		Scanner scan = new Scanner(System.in);
		int numV = scan.nextInt();
		int numE = scan.nextInt();
		Map<Integer, List<Djikstra>> graph = new HashMap<Integer, List<Djikstra>>();
		//List<Djikstra> graph = new ArrayList<Integer>()
		for(int i = numV; i > 0; i--)
		{
			List<Djikstra> nbrs = new ArrayList<Djikstra>();
			graph.put(i, nbrs);
		}

		for(int i = numE-1; i >= 0; i--)
		{
			int v1 = scan.nextInt();
			int v2 = scan.nextInt();
			int weight = scan.nextInt();

			graph.get(v1).add(new Djikstra(v2, weight, null));
			graph.get(v2).add(new Djikstra(v1, weight, null));

		}

//		for(int i : graph.keySet())
//		{
//			System.out.print(i + ": ");
//			List<Djikstra> list = graph.get(i);
//			for(int c = 0; c < list.size(); c++)
//			{
//				System.out.print(list.get(c).getNodeNumber + " ");
//			}
//			System.out.println();
//		}
		djikstra(graph, numV);

//		watch.stop();
//		System.out.println(watch);
	}

	private static void djikstra(Map<Integer, List<Djikstra>> graph, int goal)
	{
		PriorityQueue<Djikstra> queue = new PriorityQueue<Djikstra>();
		boolean[] visited = new boolean[graph.size()];

		Djikstra start = new Djikstra(1, 0, null);
		queue.add(start);

		while (!queue.isEmpty())
		{
			Djikstra node = queue.poll();
			int nodeNum = node.getNodeNumber();
			if(nodeNum == goal)
			{
				List<Djikstra> prevs = new ArrayList<Djikstra>();
				prevs.add(node);
				Djikstra prev = node.getPrev();
				//StringBuilder output = new StringBuilder("" + nodeNum);
				while(prev != null)
				{
					//output.insert(0, prev.getNodeNumber() + " ");
					prevs.add(prev);
					prev = prev.getPrev();
				}
				for(int i = prevs.size()-1; i > 0; i--)
					System.out.print(prevs.get(i) + " ");
				System.out.println(prevs.get(0));
				return;
			}

			visited[nodeNum-1] = true;
			List<Djikstra> neighbors = graph.get(nodeNum);
			int size = neighbors.size();
			for(int i = size-1; i >= 0; i--)
			{
				Djikstra nbr = neighbors.get(i);
				if(visited[nbr.getNodeNumber()-1] == false)
				{
					int cost = node.getCost() + nbr.getCost();
					Djikstra nextNode = new Djikstra(nbr.getNodeNumber(), cost, node);
					queue.add(nextNode);
				}
			}
		}

		System.out.println(-1);

	}

	private static boolean isVertexANeighbor(Map<Integer, List<Integer[]>> graph, int v1, int v2, int weight)
	{
		//System.out.println(v1);
		List<Integer[]> neighbors = graph.get(v1);
		if(neighbors == null)
			return false;
		for(int n = 0; n < neighbors.size(); n++)
		{
			Integer[] neighbor = neighbors.get(n);
			if(neighbor[0] == v2)
			{
				if(weight < neighbor[1])
				{
					neighbor[1] = weight;
					neighbors.set(n, neighbor);
					graph.put(v1, neighbors);
					return true;
				}

			}

		}

		return false;
	}

}


import java.util.*;
import java.util.PriorityQueue;

public class Djikstra implements Comparable<Djikstra>
{
	private int number;
	private int cost;
	private Djikstra prev;

	public Djikstra(int num, int c, Djikstra previous)
	{
		number = num;
		cost = c;
		prev = previous;
	}

	public int getNodeNumber()
	{
		return number;
	}

	public int getCost()
	{
		return cost;
	}

	public void setCost(int num)
	{
		cost += num;
	}

	public Djikstra getPrev()
	{
		return prev;
	}

	@Override
	public int compareTo(Djikstra o) {
		return this.cost - o.getCost();
	}

	public static void main (String[] args)
	{
		//String s = "5 6 1 2 2 2 5 5 2 3 4 1 4 1 4 3 3 3 5 1";
		/*
		 * 5 4
			1 2 2
			2 3 4
			1 4 1
			4 3 3
		 */
		Scanner scan = new Scanner(System.in);
		int numV = scan.nextInt();
		int numE = scan.nextInt();
		int[][] graph = new int[numV][numV];
		for(int i = 0; i < numE; i++)
		{
			int edgeA = scan.nextInt();
			int edgeB = scan.nextInt();
			int weight = scan.nextInt();
			if(graph[edgeA-1][edgeB-1] == 0)
				graph[edgeA-1][edgeB-1] = weight;
			else
			{
				if(weight < graph[edgeA-1][edgeB-1])
					graph[edgeA-1][edgeB-1] = weight;
			}
		}

//		for(int r = 0; r < graph.length; r++)
//		{
//			for(int c = 0; c <graph[r].length; c++)
//			{
//				System.out.print(graph[r][c] + " ");
//			}
//			System.out.println();
//		}
		djikstra(graph, numV);
		//System.out.println("gyugyuguy");
	}

	private static void djikstra(int[][] graph, int goal)
	{
		PriorityQueue<Djikstra> queue = new PriorityQueue<Djikstra>();
		boolean[] visited = new boolean[graph.length];
		//System.out.println(Arrays.toString(visited));

		Djikstra start = new Djikstra(1, 0, null);
		queue.add(start);

		while (!queue.isEmpty())
		{
			Djikstra node = queue.poll();
			if(visited[node.getNodeNumber()-1])
				continue;
			if(node.getNodeNumber() == goal)
			{
				Djikstra prev = node.getPrev();
				String output = "" + node.getNodeNumber();
				while(prev != null)
				{
					output = prev.getNodeNumber() + " " + output;
					prev = prev.getPrev();
				}
				System.out.println(output.trim());
				return;
			}

			visited[node.getNodeNumber()-1] = true;
			for(int i = 0; i < graph[node.getNodeNumber()-1].length; i++)
			{
				//System.out.println(graph[node.getNodeNumber()-1][i]);
				if(graph[node.getNodeNumber()-1][i] != 0 && i != node.getNodeNumber()-1)
				{
					//System.out.println(visited[i]);
					if(visited[i] == false)
					{
						//System.out.println("jiojoijoi");
						int cost = node.getCost() + graph[node.getNodeNumber()-1][i];
						Djikstra nextNode = new Djikstra(i+1, cost, node);
						//System.out.println(nextNode.getNodeNumber());
						queue.add(nextNode);
					}

				}

			}
		}
		System.out.println(-1);

	}

}


import java.util.*;

public class Djikstra implements Comparable<Djikstra>
{
	private int number;
	private int cost;
	private Djikstra prev;
	//private boolean visited;
	// 10^6 * 10^5 = 10^11 = not fit in an integer

	public Djikstra(int num, int c, Djikstra previous)
	{
		number = num;
		cost = c;
		prev = previous;
		//visited = false;
	}

	public int getNodeNumber()
	{
		return number;
	}

	public int getCost()
	{
		return cost;
	}

	public void setCost(int num)
	{
		cost += num;
	}

	public Djikstra getPrev()
	{
		return prev;
	}

//	public boolean checkVisited()
//	{
//		return visited;
//	}
//
//	public void visit(int num)
//	{
//		cost += num;
//	}

	@Override
	public int compareTo(Djikstra o) {
		return this.cost - o.getCost();
	}

	public static void main (String[] args)
	{
		//String s = "5 6 1 2 2 2 5 5 2 3 4 1 4 1 4 3 3 3 5 1";
		Scanner scan = new Scanner(System.in);
		int numV = scan.nextInt();
		int numE = scan.nextInt();
		//int[][] graph = new int[numV][numV];
		Map<Integer, List<Integer[]>> graph = new HashMap<Integer, List<Integer[]>>();
		for(int i = 1; i <= numV; i++)
		{
			List<Integer[]> nbrs = new ArrayList<Integer[]>();
			graph.put(i, nbrs);
		}

		for(int i = 0; i < numE; i++)
		{
			int v1 = scan.nextInt();
			int v2 = scan.nextInt();
			int weight = scan.nextInt();

			//if(!isVertexANeighbor(graph, v1, v2, weight))
			{
				Integer[] arr = {v2, weight};
				graph.get(v1).add(arr);
			}

			//if(!isVertexANeighbor(graph, v2, v1, weight))
			{
				Integer[] arr = {v1, weight};
				graph.get(v2).add(arr);
			}

//			if(graph[edgeA-1][edgeB-1] == 0)
//			{
//				graph[edgeA-1][edgeB-1] = weight;
//				graph[edgeB-1][edgeA-1] = weight;
//			}
//
//			else
//			{
//				if(weight < graph[edgeA-1][edgeB-1])
//				{
//					graph[edgeA-1][edgeB-1] = weight;
//					graph[edgeB-1][edgeA-1] = weight;
//				}
//
//			}
		}

//		for(int i : graph.keySet())
//		{
//			System.out.print(i + ": ");
//			List<Integer[]> list = graph.get(i);
//			for(int c = 0; c < list.size(); c++)
//			{
//				System.out.print(Arrays.toString(list.get(c)) + " ");
//			}
//			System.out.println();
//		}
		djikstra(graph, numV);
	}

	private static void djikstra(Map<Integer, List<Integer[]>> graph, int goal)
	{
		PriorityQueue<Djikstra> queue = new PriorityQueue<Djikstra>();
		boolean[] visited = new boolean[graph.size()];

		Djikstra start = new Djikstra(1, 0, null);
		queue.add(start);

		while (!queue.isEmpty())
		{
			Djikstra node = queue.poll();
			//System.out.println(node.getNodeNumber());
			if(visited[node.getNodeNumber()-1])
				continue;
			if(node.getNodeNumber() == goal)
			{
				Djikstra prev = node.getPrev();
				String output = "" + node.getNodeNumber();
				while(prev != null)
				{
					output = prev.getNodeNumber() + " " + output;
					prev = prev.getPrev();
				}
				System.out.println(output.trim());
				return;
			}

			visited[node.getNodeNumber()-1] = true;
			List<Integer[]> neighbors = graph.get(node.getNodeNumber());
			for(int i = 0; i < neighbors.size(); i++)
			{
				Integer[] nbr = neighbors.get(i);
				//System.out.println(nbr[0]);
				if(visited[nbr[0]-1] == false)
				{
					//System.out.println("VISITED IS FALSE");
					int cost = node.getCost() + nbr[1];
					Djikstra nextNode = new Djikstra(nbr[0], cost, node);
					queue.add(nextNode);
				}

			}
		}

		System.out.println(-1);

	}

	private static boolean isVertexANeighbor(Map<Integer, List<Integer[]>> graph, int v1, int v2, int weight)
	{
		//System.out.println(v1);
		List<Integer[]> neighbors = graph.get(v1);
		if(neighbors == null)
			return false;
		for(int n = 0; n < neighbors.size(); n++)
		{
			Integer[] neighbor = neighbors.get(n);
			if(neighbor[0] == v2)
			{
				if(weight < neighbor[1])
				{
					neighbor[1] = weight;
					neighbors.set(n, neighbor);
					graph.put(v1, neighbors);
					return true;
				}

			}

		}

		return false;
	}

}


import java.util.*;

public class Djikstra implements Comparable<Djikstra>
{
	private int number;
	private int cost;
	private Djikstra prev;
	// 10^6 * 10^5 = 10^11 = not fit in an integer

	public Djikstra(int num, int c, Djikstra previous)
	{
		number = num;
		cost = c;
		prev = previous;
	}

	public int getNodeNumber()
	{
		return number;
	}

	public int getCost()
	{
		return cost;
	}

	public void setCost(int num)
	{
		cost += num;
	}

	public Djikstra getPrev()
	{
		return prev;
	}

	@Override
	public int compareTo(Djikstra o) {
		return this.cost - o.getCost();
	}

	public static void main (String[] args)
	{
		//String s = "5 6 1 2 2 2 5 5 2 3 4 1 4 1 4 3 3 3 5 1";
		Scanner scan = new Scanner(System.in);
		int numV = scan.nextInt();
		int numE = scan.nextInt();
		Map<Integer, List<Djikstra>> graph = new HashMap<Integer, List<Djikstra>>();
		for(int i = 1; i <= numV; i++)
		{
			List<Djikstra> nbrs = new ArrayList<Djikstra>();
			graph.put(i, nbrs);
		}

		for(int i = 0; i < numE; i+=2)
		{
			int v1 = scan.nextInt();
			int v2 = scan.nextInt();
			int weight = scan.nextInt();

			graph.get(v1).add(new Djikstra(v2, weight, null));
			graph.get(v2).add(new Djikstra(v1, weight, null));

			v1 = scan.nextInt();
			v2 = scan.nextInt();
			weight = scan.nextInt();

			graph.get(v1).add(new Djikstra(v2, weight, null));
			graph.get(v2).add(new Djikstra(v1, weight, null));
		}

		if(numE % 2 == 1)
		{
			int v1 = scan.nextInt();
			int v2 = scan.nextInt();
			int weight = scan.nextInt();

			graph.get(v1).add(new Djikstra(v2, weight, null));
			graph.get(v2).add(new Djikstra(v1, weight, null));
		}

//		for(int i : graph.keySet())
//		{
//			System.out.print(i + ": ");
//			List<Djikstra> list = graph.get(i);
//			for(int c = 0; c < list.size(); c++)
//			{
//				System.out.print(list.get(c).getNodeNumber + " ");
//			}
//			System.out.println();
//		}
		djikstra(graph, numV);
	}

	private static void djikstra(Map<Integer, List<Djikstra>> graph, int goal)
	{
		PriorityQueue<Djikstra> queue = new PriorityQueue<Djikstra>();
		boolean[] visited = new boolean[graph.size()];

		Djikstra start = new Djikstra(1, 0, null);
		queue.add(start);

		while (!queue.isEmpty())
		{
			Djikstra node = queue.poll();
			int nodeNum = node.getNodeNumber();
			if(visited[nodeNum-1])
				continue;
			if(nodeNum == goal)
			{
				Djikstra prev = node.getPrev();
				String output = "" + nodeNum;
				while(prev != null)
				{
					output = prev.getNodeNumber() + " " + output;
					prev = prev.getPrev();
				}
				System.out.println(output.trim());
				return;
			}

			visited[nodeNum-1] = true;
			List<Djikstra> neighbors = graph.get(nodeNum);
			for(int i = 0; i < neighbors.size(); i++)
			{
				Djikstra nbr = neighbors.get(i);
				if(visited[nbr.getNodeNumber()-1] == false)
				{
					int cost = node.getCost() + nbr.getCost();
					Djikstra nextNode = new Djikstra(nbr.getNodeNumber(), cost, node);
					queue.add(nextNode);
				}

			}
		}

		System.out.println(-1);

	}

	private static boolean isVertexANeighbor(Map<Integer, List<Integer[]>> graph, int v1, int v2, int weight)
	{
		//System.out.println(v1);
		List<Integer[]> neighbors = graph.get(v1);
		if(neighbors == null)
			return false;
		for(int n = 0; n < neighbors.size(); n++)
		{
			Integer[] neighbor = neighbors.get(n);
			if(neighbor[0] == v2)
			{
				if(weight < neighbor[1])
				{
					neighbor[1] = weight;
					neighbors.set(n, neighbor);
					graph.put(v1, neighbors);
					return true;
				}

			}

		}

		return false;
	}

}