#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;
LL gcd(LL a, LL b)
{
while(a)
swap(a, b%=a);
return b;
}
LL lcm(LL a, LL b)
{
return a/gcd(a,b)*b;
}
class BottlesOnShelf { public:
int getNumBroken(int N, vector <int> left, vector <int> right, vector <int> damage)
{
int total = 0;
for(int mask=1; mask<(1<<left.size()); ++mask)
total += (bitcnt(mask)%2==0 ? -1 : +1) * hit(N, left, right, damage, mask);
return total;
}
int bitcnt(int m)
{
int c = 0;
for(; m; m>>=1)
c += (m&1);
return c;
}
LL ceil(LL x, LL q)
{
return (x + q - 1) / q * q;
}
int hit(int N, vector <int> left, vector <int> right, vector <int> damage, int mask)
{
LL theLeft = 1;
LL theRight = N+1;
LL theDamage = 1;
for(int i=0; i<left.size(); ++i)
if( mask & (1<<i) )
{
theLeft = max<LL>(theLeft, left[i]);
theRight = min<LL>(theRight, right[i]+1);
theDamage = lcm(theDamage, damage[i]);
}
return int( max(0LL, ceil(theRight,theDamage)-ceil(theLeft,theDamage)) / theDamage );
}
};
// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
{ ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
{ os << "{ ";
for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
bool ok = (Expected == Received);
if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl;
cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END verify_case(_, BottlesOnShelf().getNumBroken(N, left, right, damage));}
int main(){
CASE(0)
int N = 7;
int left_[] = {1};
vector <int> left(left_, left_+sizeof(left_)/sizeof(*left_));
int right_[] = {7};
vector <int> right(right_, right_+sizeof(right_)/sizeof(*right_));
int damage_[] = {2};
vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_));
int _ = 3;
END
CASE(1)
int N = 7;
int left_[] = {1,1};
vector <int> left(left_, left_+sizeof(left_)/sizeof(*left_));
int right_[] = {7,7};
vector <int> right(right_, right_+sizeof(right_)/sizeof(*right_));
int damage_[] = {2,3};
vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_));
int _ = 4;
END
CASE(2)
int N = 7;
int left_[] = {1,1,1};
vector <int> left(left_, left_+sizeof(left_)/sizeof(*left_));
int right_[] = {7,7,7};
vector <int> right(right_, right_+sizeof(right_)/sizeof(*right_));
int damage_[] = {2,3,6};
vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_));
int _ = 4;
END
CASE(3)
int N = 10;
int left_[] = {1,6};
vector <int> left(left_, left_+sizeof(left_)/sizeof(*left_));
int right_[] = {5,10};
vector <int> right(right_, right_+sizeof(right_)/sizeof(*right_));
int damage_[] = {1,7};
vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_));
int _ = 6;
END
CASE(4)
int N = 5;
int left_[] = {4};
vector <int> left(left_, left_+sizeof(left_)/sizeof(*left_));
int right_[] = {4};
vector <int> right(right_, right_+sizeof(right_)/sizeof(*right_));
int damage_[] = {7};
vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_));
int _ = 0;
END
CASE(5)
int N = 1000000000;
int left_[] = {1};
vector <int> left(left_, left_+sizeof(left_)/sizeof(*left_));
int right_[] = {1000000000};
vector <int> right(right_, right_+sizeof(right_)/sizeof(*right_));
int damage_[] = {1};
vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_));
int _ = 1000000000;
END
/*
CASE(6)
int N = ;
int left_[] = ;
vector <int> left(left_, left_+sizeof(left_)/sizeof(*left_));
int right_[] = ;
vector <int> right(right_, right_+sizeof(right_)/sizeof(*right_));
int damage_[] = ;
vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_));
int _ = ;
END
CASE(7)
int N = ;
int left_[] = ;
vector <int> left(left_, left_+sizeof(left_)/sizeof(*left_));
int right_[] = ;
vector <int> right(right_, right_+sizeof(right_)/sizeof(*right_));
int damage_[] = ;
vector <int> damage(damage_, damage_+sizeof(damage_)/sizeof(*damage_));
int _ = ;
END
*/
}
// END CUT HERE