#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
using IntPair = pair<int, int>;
int main()
{
int n,sz, mx = 0, mx2 = 0;
cin >> n >> sz;
vector<int> v(n + 1);
IntPair ans;
for (int i = 0; i < sz; i++)
{
int a, b, cnt = 0;
cin >> a >> b;
if (b - a > mx)
{
mx = b - a;
ans.first = i + 1;
}
for (int j = a; j <= b; j++)
{
if (v[j] == 0)
{
v[j] = i + 1;
cnt++;
}
}
if (cnt > mx2)
{
mx2 = cnt;
ans.second = i + 1;
}
cnt = 0;
}
cout << ans.first << '\n' << ans.second;
}