#if 1
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
#include <array>
#include <deque>
#include <algorithm>
#include <utility>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <numeric>
#include <assert.h>
#include <bitset>
#include <list>
#include <cmath>

using namespace std;
const double PI = 3.141592653589793238462643383279502884197169399375105820974944;

int32_t N, M;
int64_t D[109000];
std::pair<int, std::pair<int64_t, int>> range[109000];
const int64_t INF = 1090001000000000;
int main()
{
	using std::endl;

	cin >> N >> M;
	for (size_t i = 0; i < M; i++)
	{
		int a, b, c;
		cin >> a >> b >> c; --a; 
		range[i].first = a;
		range[i].second.first = c;
		range[i].second.second = b;
	}
	std::sort(range, range + M);
	std::priority_queue<std::pair<int64_t, int>, std::vector<std::pair<int64_t, int>>, std::greater<>> que;

	for (size_t i = 0; i < N; i++)
	{
		D[i] = INF;
	}
	D[0] = 0;
	que.push({ 0, 0 });
	auto iter = range;
	auto iterend = range;
	while (!que.empty())
	{
		auto pos = que.top(); que.pop();

		while (iterend != range+M && iterend->first <= pos.second)
		{
			++iterend;
		}
		while (iter != iterend && iter->second.second <= pos.second)
		{
			++iter;
		}

		while (iter != iterend)
		{
			auto newcost = pos.first + iter->second.first;
			if (D[iter->second.second - 1] > newcost) {
				D[iter->second.second - 1] = newcost;
				que.push({ pos.first + iter->second.first, iter->second.second - 1 });
			}

			++iter;
		}


	}

	if (D[N - 1] == INF) {
		D[N - 1] = -1;
	}
	cout << D[N - 1] << endl;

	return 0;
}
#endif

#if 0
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
#include <array>
#include <deque>
#include <algorithm>
#include <utility>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <numeric>
#include <assert.h>
#include <bitset>
#include <list>
#include <cmath>

using namespace std;
#define all_range(C) std::begin(C), std::end(C)
const double PI = 3.141592653589793238462643383279502884197169399375105820974944;

int32_t N;
int32_t A[100000];
int32_t B[100000];
int32_t Asort[100000];
int32_t Bsort[100000];
bool isok[3][300000][20];

int main()
{
	using std::endl;

	cin >> N;
	for (size_t i = 0; i < N; i++)
	{
		cin >> A[i];
	}
	for (size_t i = 0; i < N; i++)
	{
		cin >> B[i];
	}

	for (size_t i = 0; i < N; i++)
	{
		Asort[i] = A[i];
		Bsort[i] = B[i];
	}

	std::sort(Asort, Asort + N);
	std::sort(Bsort, Bsort + N);
	Bsort[N] = 1100000000;

	if(N==2){
		bool ok2 = true;
		for (size_t i = 0; i < N; i++)
		{
			ok2 &= B[i] >= A[i];
		}
		if (ok2)
		{
			cout << "Yes" << endl;
		}
		else {
			cout << "No" << endl;
		};
		return 0;
	}

	isok[0][0][0] = true;
	for (size_t i = 0; i < N; i++)
	{
		if (i > 0) { isok[0][i][0] = Bsort[i - 1] >= Asort[i]; }
		isok[1][i][0] = Bsort[i] >= Asort[i];
		isok[2][i][0] = Bsort[i+1] >= Asort[i];
	}
	for (size_t i = N; i < 3*N; i++)
	{
		isok[0][i][0] = true;
		isok[1][i][0] = true;
		isok[2][i][0] = true;
	}

	for (size_t b = 1; b < 20; b++)
	{
		for (size_t i = 0; i < 2 * N; i++)
		{
			auto next = i + (1 << (b - 1));
			if (next >= 2*N) {
				next = i;
			}
			isok[0][i][b] = isok[0][i][b-1] & isok[0][next][b-1];
			isok[1][i][b] = isok[1][i][b-1] & isok[1][next][b-1];
			isok[2][i][b] = isok[2][i][b-1] & isok[2][next][b-1];
		}
	}

	for (size_t cou = 0; cou < N; cou++)
	{
		if (B[cou] < A[cou]) { continue; }
		auto aindex = std::upper_bound(Asort, Asort + N, A[cou]) - Asort - 1;
		auto bindex = std::lower_bound(Bsort, Bsort + N, B[cou]) - Bsort;

		bool ok = true;
		if (aindex < bindex)
		{
			int i = 0;
			for (int b = 20 - 1; b >= 0; b--)
			{
				auto next = i + (1 << (b ));
				if (aindex < next) { continue; }

				ok &= isok[1][i][b];
				i = next;
			}
			assert(i == aindex);
			++i;
			for (int b = 20 - 1; b >= 0; b--)
			{
				auto next = i + (1 << (b ));
				if (bindex < next) { continue; }

				ok &= isok[0][i][b];
				i = next;
			}
			assert(i == bindex);
			++i;
			for (int b = 20 - 1; b >= 0; b--)
			{
				auto next = i + (1 << (b ));
				if (N < next) { continue; }

				ok &= isok[1][i][b];
				i = next;
			}
			assert(i == N);
		}
		else
		{
			int i = 0;
			for (int b = 20 - 1; b >= 0; b--)
			{
				auto next = i + (1 << (b ));
				if (bindex < next) { continue; }

				ok &= isok[1][i][b];
				i = next;
			}
			assert(i == bindex);
			if (aindex != bindex)
			{
				for (int b = 20 - 1; b >= 0; b--)
				{
					auto next = i + (1 << (b));
					if (aindex < next + 1) { continue; }

					ok &= isok[2][i][b];
					i = next;
				}
				assert(i+1 == aindex);
				++i;
			}
			++i;
			for (int b = 20 - 1; b >= 0; b--)
			{
				auto next = i + (1 << (b ));
				if (N < next) { continue; }

				ok &= isok[1][i][b];
				i = next;
			}
			assert(i == N);

			bool ok2 = true;
			for (size_t i = 0; i < N; i++)
			{
				ok2 &= Bsort[i] >= Asort[i];
			}
			ok = ok2;

		}

		if (ok)
		{
			cout << "Yes" << endl;
			return 0;
		}

	}



	cout << "No" << endl;
	return 0;
}
#endif


Measured memory_bytes: 5364