/*
ID: anonymo14
TASK: wormhole
LANG: C++                 
*/
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pair<int,int>> vpii;

#define F first
#define S second
#define PU push
#define PUF push_front
#define PUB push_back
#define PO pop
#define POF pop_front
#define POB pop_back

#define REP(i,a,b) for(int i=a; i<=b; i++)

#define MOD 1000000007

void solve(int test_case) {
	ll a,b;
	cin>>a>>b;
	ll dc = __gcd(a,b);
	int cnt = 1;
	for(ll i=2;i*i<=dc;i++) {
		if(dc%i==0) {
			cnt++;
			while(dc%i==0)dc/=i;
		}
	}
	if(dc!=1)cnt++;
	cout<<cnt;
}

int main() {
	
	////// FILE BASED IO////
	//freopen("wormhole.in", "r", stdin);
	//freopen("wormhole.out", "w", stdout);
	///////////////
	
	ios::sync_with_stdio(0);
	cin.tie(0);
	int t=1;
	//cin>>t;
	REP(i,1,t) {
		solve(i);
	}
	return 0;
}	

#include <bits/stdc++.h>
#define PI 3.14159265359
#define NIL (-1)
#define LL long long
using namespace std;
const int64_t MOD = 1e9 + 7;
const int INF = INT_MAX;

int main() {
	int N, M, L, R;
	cin >> N >> M;

	int a, b;
	a = 0;
	b = N;

	for (int i = 0; i < M; i++) {
		cin >> L >> R;
		a = max(a, L);
		b = min(b, R);
	}

	int ret = b - a;
	if (ret < 0) ret = 0;
	else ret++;

	cout << ret << endl;
}