//#include <numeric>
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <deque>
#include <string>
#include <string.h>
#include <vector>
#include <stack>
#include <queue>
#include <math.h>
#include <stdlib.h>
#include <map>
#include <set>
#include <time.h>
#include <bitset>
#include <list>
#include <limits>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vstr;
typedef vector<char> vc;
typedef vector<bool> vb;

double EPS = 1e-9;
int INF = 0x7FFFFFFF;//numeric_limits<int>::max();
long long INFF = 0x7FFFFFFFFFFFFFFF;//numeric_limits<long long>::max();
double PI = acos(-1.0);
int dirx[8] = {-1,0,0,1,-1,-1,1,1};
int diry[8] = {0,1,-1,0,-1,1,-1,1};

//#define TESTING
#ifdef TESTING
#define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define VALUE(x)
#define debug(...)
#endif

#define SIZE(v) (int)v.size()
#define FOR(a,b,c) for (int (a)=(b);(a)<(c);++(a))
#define FORN(a,b,c) for (int (a)=(b);(a)<=(c);++(a))
#define FORD(a,b,c) for (int (a)=(b);(a)>=(c);--(a))

//all zero
#define REP(i,n) FOR(i,0,n)
#define REPV(i,v) FOR(i,0,SIZE(v))
#define REPN(i,n) FORN(i,0,n)
#define REPD(i,n) FORD(i,n,0)
#define REPI(t,v,i) for(t::iterator i=v.begin();i!=v.end();i++)
#define REPI2(t1,t2,v,i) for(t1,t2::iterator i=v.begin();i!=v.end();i++)
#define MAX(a,b) a = max(a,b)
#define MIN(a,b) a = min(a,b)
#define RESET(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(),v.end()
#define ALLA(arr,sz) arr,arr+sz
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr,sz) sort(ALLA(arr,sz))
#define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
#define PERMUTE next_permutation
#define FIND(m,i) m.find(i)!=m.end()
#define UNFIND(m,i) !(FIND(m,i))

#pragma region Functions

inline void scanf(int &d1){scanf("%d",&d1);}
inline void scanf(int &d1,int &d2){scanf("%d%d",&d1,&d2);}
inline void scanf(int &d1,int &d2,int &d3){scanf("%d%d%d",&d1,&d2,&d3);}
inline void scanf(ll &d1){scanf("%I64d",&d1);}
inline void scanf(ll &d1,ll &d2){scanf("%I64d%I64d",&d1,&d2);}
inline void scanf(ll &d1,ll &d2,ll &d3){scanf("%I64d%I64d%I64d",&d1,&d2,&d3);}
inline void scanf(char *temp){scanf("%s",temp);}

inline void printf(int d1){printf("%d\n",d1);}
inline void printf(int d1,int d2){printf("%d %d\n",d1,d2);}
inline void printf(double f1){printf("%.17g\n",f1);}
inline void printf(ll l1){printf("%I64d\n",l1);}
inline void printf(ll l1,ll l2){printf("%I64d %I64d\n",l1,l2);}
inline void printfcase(int id){printf("Case #%d:",id);}

inline string tostring(int a){char x[100];sprintf(x,"%d",a); string s = x;return s;}
inline void toupperstring(string &s){REPV(i,s){if(s[i]>='a'&&s[i]<='z'){s[i]=s[i]-'a'+'A';}}}//transform(s.begin(),s.end(),s.begin(),::toupper);
inline void tolowerstring(string &s){REPV(i,s){if(s[i]>='A'&&s[i]<='Z'){s[i]=s[i]-'A'+'a';}}}//transform(s.begin(),s.end(),s.begin(),::tolower);
inline int toint(string a){char x[100];int res;strcpy(x,a.c_str());sscanf(x,"%d",&res);return res;}

inline void freopen(string sin,string sout){freopen(sin.c_str(),"r",stdin);freopen(sout.c_str(),"w",stdout);}

template<typename T1, typename T2,typename T>
inline typename T::iterator findorinsert(T &m,const T1& t1,const T2 & t2){
	typename T::iterator iter = m.find(t1);
	if(iter == m.end()){m.insert(mp(t1,t2));return m.find(t1);}
	else{return iter;}
}

template<typename T>
inline T gcd(T u, T v){while(v != 0){T r = u%v;u = v;v = r;}return u;}

#pragma endregion

#pragma region Structs & Classes

template<typename T,int n>
struct Vector{
	T data[n];
	void read(){REP(i,n){cin>>data[i];}}
	inline T operator*(const Vector<T,n> &v) const{
		T res = 0;REP(i,n){res+=data[i]*v[i];}return res;
	}
	inline bool operator<(const Vector<T,n> &v) const{
		REP(i,n){if(data[i]!=v[i]){return data[i]<v[i];}}return false;
	}
	inline bool operator==(const Vector<T,n> &v) const{
		REP(i,n){if(data[i]!=v[i]){return false;}}return true;
	}
	inline T& operator[](int id){return data[id];}
	inline T operator[](int id) const{return data[id];}
	inline int size()const{return n;}
};

typedef Vector<int,2> Vi2;
template<typename T>
inline Vector<T,2> make_vector(const T &t1,const T &t2){Vector<T,2> res = {t1,t2};return res;}
#define mv make_vector

template<typename T,int n>
struct Matrix{
	Vector<T,n> data[n];
	inline Vector<T,n> operator*(const Vector<T,n>& v) const{
		Vector<T,n> res;REP(i,n){res[i]=data[i]*v;}return res;
	}
	inline Matrix<T,n> operator*(const Matrix<T,n>& m)const{
		Matrix<T,n> res;REP(i,n){
			Vector<T,n> temp;REP(j,n){temp[j]=m[j][i];}REP(j,n){res[j][i]=data[j]*temp;}
		}
		return res;
	}
	static inline Matrix<T,n> identity(){
		Matrix<T,n> res;REP(i,n){memset(res[i].data,0,sizeof(T)*n);}REP(i,n){res[i][i]=1;}return res;
	}
	Vector<T,n>& operator[](int id){return data[id];}
	Vector<T,n> operator[](int id)const{return data[id];}
	int size() const{return n;}
};

template<typename T,int n>
class BIT{
public:
	BIT(int size = n){RESET(bit,0);this->size = size;}
	BIT(T* data,int size = n){
		this->size = size;for (int i=1;i<=size;i++){
			bit[i]=data[i-1];for (int j=i-1; j>i-lowbit(i); j--){bit[i]+=bit[j];}
		}
	}
	void edit(int i, T delta){for (int j = i+1; j <= size; j += lowbit(j)){bit[j] += delta;}}
	T sum(int k){
		T ans = 0;for (int i = k+1; i > 0; i -= lowbit(i)){ans += bit[i];}return ans;
	}
private:
	T bit[n+1];int size;
	inline int lowbit(int x) const{return x&(-x);}
};

class Prime{
public:
	Prime(int num){bits.resize(num+1,true);sieve();collect();}
	bool is_prime(int num) const{return bits[num];}
	int find1factor(int num) const{
		REPV(i,results){if(num%results[i] == 0){return results[i];}}return num;
	}
private:
	//start from 0
	vector<bool> bits;vector<int> results;
	void sieve(){
		bits[0] = false;bits[1] = false;
		for(int i = 2;i < SIZE(bits);++i){
			if(bits[i] == true){
				for(int j = 2;;++j){
					int v2 = j*i;if(v2 >= SIZE(bits)){break;}bits[v2] = false;
				}
			}
		}
	}
	void collect(){
		for(int i = 0;i < SIZE(bits);++i){if(bits[i]){results.push_back(i);}}
	}
};

template<int n,int m>
struct Graph{
	int head[n],edge_id;
	struct{
		int to,next;
	}edges[m];
	void init(int num = n){edge_id = 0;memset(head,-1,sizeof(int)*num);}
	int add(int from,int to){
		edges[edge_id].next = head[from];edges[edge_id].to = to;
		head[from] = edge_id;edge_id++;return head[from];
	}
	inline int next(int edge) const{return edges[edge].next;}
	inline int to(int edge) const{return edges[edge].to;}
};

#define REPG(g,id,e) for(int e = g.head[id];e != -1;e = g.next(e))

#pragma endregion

#define UNORDERED
#ifdef UNORDERED
#include <unordered_map>
#include <unordered_set>
typedef unordered_set<int> hashint;
template<class T>
inline void hash_combine(std::size_t & seed, const T & v){
	std::hash<T> hasher;seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
template<typename T,int n>
struct VectorHash{
	inline std::size_t operator()(const Vector<T,n> &v) const{
		std::size_t seed = 0;REP(i,n){hash_combine(seed,v[i]);}return seed;
	}
};
#endif

const int max_n = 10000;
ll data[max_n+1];
int n;

int main(){
	scanf(n);
	REP(i,n){
		scanf(data[i]);
	}
	data[n] = INF;
	n++;
	ll res = 0;
	int start = 1,end = 1;
	while(true){
		for(;start<n;start++){
			if(data[start-1]>data[start]){
				break;
			}
		}
		if(start == n){
			break;
		}
		for(end = max(end,start);end < n;end++){
			if(data[end]<data[end+1]){
				break;
			}
		}
		ll actions = min(data[start-1]-data[start],data[end+1]-data[end]);
		res += actions;
		FORN(i,start,end){
			data[i] += actions;
		}
	}
	printf(res);
	return 0;
}


//#include <numeric>
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <deque>
#include <string>
#include <string.h>
#include <vector>
#include <stack>
#include <queue>
#include <math.h>
#include <stdlib.h>
#include <map>
#include <set>
#include <time.h>
#include <bitset>
#include <list>
#include <limits>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vstr;
typedef vector<char> vc;
typedef vector<bool> vb;

double EPS = 1e-9;
int INF = 0x7FFFFFFF;//numeric_limits<int>::max();
long long INFF = 0x7FFFFFFFFFFFFFFF;//numeric_limits<long long>::max();
double PI = acos(-1.0);
int dirx[8] = {-1,0,0,1,-1,-1,1,1};
int diry[8] = {0,1,-1,0,-1,1,-1,1};

//#define TESTING
#ifdef TESTING
#define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define VALUE(x)
#define debug(...)
#endif

#define SIZE(v) (int)v.size()
#define FOR(a,b,c) for (int (a)=(b);(a)<(c);++(a))
#define FORN(a,b,c) for (int (a)=(b);(a)<=(c);++(a))
#define FORD(a,b,c) for (int (a)=(b);(a)>=(c);--(a))

//all zero
#define REP(i,n) FOR(i,0,n)
#define REPV(i,v) FOR(i,0,SIZE(v))
#define REPN(i,n) FORN(i,0,n)
#define REPD(i,n) FORD(i,n,0)
#define REPI(t,v,i) for(t::iterator i=v.begin();i!=v.end();i++)
#define REPI2(t1,t2,v,i) for(t1,t2::iterator i=v.begin();i!=v.end();i++)
#define MAX(a,b) a = max(a,b)
#define MIN(a,b) a = min(a,b)
#define RESET(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(),v.end()
#define ALLA(arr,sz) arr,arr+sz
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr,sz) sort(ALLA(arr,sz))
#define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
#define PERMUTE next_permutation
#define FIND(m,i) m.find(i)!=m.end()
#define UNFIND(m,i) !(FIND(m,i))

#pragma region Functions

inline void scanf(int &d1){scanf("%d",&d1);}
inline void scanf(int &d1,int &d2){scanf("%d%d",&d1,&d2);}
inline void scanf(int &d1,int &d2,int &d3){scanf("%d%d%d",&d1,&d2,&d3);}
inline void scanf(ll &d1){scanf("%I64d",&d1);}
inline void scanf(ll &d1,ll &d2){scanf("%I64d%I64d",&d1,&d2);}
inline void scanf(ll &d1,ll &d2,ll &d3){scanf("%I64d%I64d%I64d",&d1,&d2,&d3);}
inline void scanf(char *temp){scanf("%s",temp);}

inline void printf(int d1){printf("%d\n",d1);}
inline void printf(int d1,int d2){printf("%d %d\n",d1,d2);}
inline void printf(double f1){printf("%.17g\n",f1);}
inline void printf(ll l1){printf("%I64d\n",l1);}
inline void printf(ll l1,ll l2){printf("%I64d %I64d\n",l1,l2);}
inline void printfcase(int id){printf("Case #%d:",id);}

inline string tostring(int a){char x[100];sprintf(x,"%d",a); string s = x;return s;}
inline void toupperstring(string &s){REPV(i,s){if(s[i]>='a'&&s[i]<='z'){s[i]=s[i]-'a'+'A';}}}//transform(s.begin(),s.end(),s.begin(),::toupper);
inline void tolowerstring(string &s){REPV(i,s){if(s[i]>='A'&&s[i]<='Z'){s[i]=s[i]-'A'+'a';}}}//transform(s.begin(),s.end(),s.begin(),::tolower);
inline int toint(string a){char x[100];int res;strcpy(x,a.c_str());sscanf(x,"%d",&res);return res;}

inline void freopen(string sin,string sout){freopen(sin.c_str(),"r",stdin);freopen(sout.c_str(),"w",stdout);}

template<typename T1, typename T2,typename T>
inline typename T::iterator findorinsert(T &m,const T1& t1,const T2 & t2){
	typename T::iterator iter = m.find(t1);
	if(iter == m.end()){m.insert(mp(t1,t2));return m.find(t1);}
	else{return iter;}
}

template<typename T>
inline T gcd(T u, T v){while(v != 0){T r = u%v;u = v;v = r;}return u;}

#pragma endregion

#pragma region Structs & Classes

template<typename T,int n>
struct Vector{
	T data[n];
	void read(){REP(i,n){cin>>data[i];}}
	inline T operator*(const Vector<T,n> &v) const{
		T res = 0;REP(i,n){res+=data[i]*v[i];}return res;
	}
	inline bool operator<(const Vector<T,n> &v) const{
		REP(i,n){if(data[i]!=v[i]){return data[i]<v[i];}}return false;
	}
	inline bool operator==(const Vector<T,n> &v) const{
		REP(i,n){if(data[i]!=v[i]){return false;}}return true;
	}
	inline T& operator[](int id){return data[id];}
	inline T operator[](int id) const{return data[id];}
	inline int size()const{return n;}
};

typedef Vector<int,2> Vi2;
template<typename T>
inline Vector<T,2> make_vector(const T &t1,const T &t2){Vector<T,2> res = {t1,t2};return res;}
#define mv make_vector

template<typename T,int n>
struct Matrix{
	Vector<T,n> data[n];
	inline Vector<T,n> operator*(const Vector<T,n>& v) const{
		Vector<T,n> res;REP(i,n){res[i]=data[i]*v;}return res;
	}
	inline Matrix<T,n> operator*(const Matrix<T,n>& m)const{
		Matrix<T,n> res;REP(i,n){
			Vector<T,n> temp;REP(j,n){temp[j]=m[j][i];}REP(j,n){res[j][i]=data[j]*temp;}
		}
		return res;
	}
	static inline Matrix<T,n> identity(){
		Matrix<T,n> res;REP(i,n){memset(res[i].data,0,sizeof(T)*n);}REP(i,n){res[i][i]=1;}return res;
	}
	Vector<T,n>& operator[](int id){return data[id];}
	Vector<T,n> operator[](int id)const{return data[id];}
	int size() const{return n;}
};

template<typename T,int n>
class BIT{
public:
	BIT(int size = n){RESET(bit,0);this->size = size;}
	BIT(T* data,int size = n){
		this->size = size;for (int i=1;i<=size;i++){
			bit[i]=data[i-1];for (int j=i-1; j>i-lowbit(i); j--){bit[i]+=bit[j];}
		}
	}
	void edit(int i, T delta){for (int j = i+1; j <= size; j += lowbit(j)){bit[j] += delta;}}
	T sum(int k){
		T ans = 0;for (int i = k+1; i > 0; i -= lowbit(i)){ans += bit[i];}return ans;
	}
private:
	T bit[n+1];int size;
	inline int lowbit(int x) const{return x&(-x);}
};

class Prime{
public:
	Prime(int num){bits.resize(num+1,true);sieve();collect();}
	bool is_prime(int num) const{return bits[num];}
	int find1factor(int num) const{
		REPV(i,results){if(num%results[i] == 0){return results[i];}}return num;
	}
private:
	//start from 0
	vector<bool> bits;vector<int> results;
	void sieve(){
		bits[0] = false;bits[1] = false;
		for(int i = 2;i < SIZE(bits);++i){
			if(bits[i] == true){
				for(int j = 2;;++j){
					int v2 = j*i;if(v2 >= SIZE(bits)){break;}bits[v2] = false;
				}
			}
		}
	}
	void collect(){
		for(int i = 0;i < SIZE(bits);++i){if(bits[i]){results.push_back(i);}}
	}
};

template<int n,int m>
struct Graph{
	int head[n],edge_id;
	struct{
		int to,next;
	}edges[m];
	void init(int num = n){edge_id = 0;memset(head,-1,sizeof(int)*num);}
	int add(int from,int to){
		edges[edge_id].next = head[from];edges[edge_id].to = to;
		head[from] = edge_id;edge_id++;return head[from];
	}
	inline int next(int edge) const{return edges[edge].next;}
	inline int to(int edge) const{return edges[edge].to;}
};

#define REPG(g,id,e) for(int e = g.head[id];e != -1;e = g.next(e))

#pragma endregion

#define UNORDERED
#ifdef UNORDERED
#include <unordered_map>
#include <unordered_set>
typedef unordered_set<int> hashint;
template<class T>
inline void hash_combine(std::size_t & seed, const T & v){
	std::hash<T> hasher;seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
template<typename T,int n>
struct VectorHash{
	inline std::size_t operator()(const Vector<T,n> &v) const{
		std::size_t seed = 0;REP(i,n){hash_combine(seed,v[i]);}return seed;
	}
};
#endif

const int max_n = 100000;
ll data[max_n+1];
int n;

int main(){
	scanf(n);
	REP(i,n){
		scanf(data[i]);
	}
	data[n] = INF;
	n++;
	ll res = 0;
	int start = 1,end = 1;
	while(true){
		for(;start<n;start++){
			if(data[start-1]>data[start]){
				break;
			}
		}
		if(start == n){
			break;
		}
		for(end = max(end,start);end < n;end++){
			if(data[end]<data[end+1]){
				break;
			}
		}
		ll actions = min(data[start-1]-data[start],data[end+1]-data[end]);
		res += actions;
		FORN(i,start,end){
			data[i] += actions;
		}
	}
	printf(res);
	return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
int des(int a[],int n,int tool)
{
	for(int i=n;i<tool-1;i++)
		if(a[i]>a[i+1])
			return i;
}

int inc(int a[],int n,int tool)
{
	for(int i=n;i<tool-1;i++)
		if(a[i]<a[i+1])
			return i;
}

int main()
{
	int n;
	cin >> n;
	int a[100011];
	int moves = 0;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	int sood;
	int nooz;
	for (int i = 0; i < n; i++)
	{
		nooz = des(a, i, n);
		sood = inc(a, nooz, n);
		moves += a[nooz] - a[sood];
		i=sood;
	}
	cout << moves << endl;
	return 0;
}


#include <iostream>
using namespace std;
int des(int a[],int n,int tool)
{
    for(int i=n;i<tool-1;i++)
        if(a[i]>a[i+1])
            return i;
}

int inc(int a[],int n,int tool)
{
    for(int i=n;i<tool-1;i++)
        if(a[i]<a[i+1])
            return i;
}

int main()
{
    int n;
    cin >> n;
    int a[100011];
    int moves = 0;
    for (int i = 0; i < n; i++)
        cin >> a[i];
    int sood;
    int nooz;
    for (int i = 0; i < n; i++)
    {
        nooz = des(a, i, n);
        sood = inc(a, nooz, n);
        moves += a[nooz] - a[sood];
        i=sood;
    }
    cout << moves << endl;
    return 0;
}


#include <iostream>
using namespace std;
int des(int a[],int n,int tool)
{
	for(int i=n;i<tool-1;i++)
		if(a[i]>a[i+1])
			return i;
}

int inc(int a[],int n,int tool)
{
	for(int i=n;i<tool-1;i++)
		if(a[i]<a[i+1])
			return i;
}

int main()
{
	int n;
	cin >> n;
	int a[100011];
	int moves = 0;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	int sood;
	int nooz;
	for (int i = 0; i < n; i++)
	{
		nooz = des(a, i, n);
		sood = inc(a, nooz, n);
		moves += a[nooz] - a[sood];
		i=sood;
	}
	cout << moves << endl;
	return 0;
}


//be name khoda

#include <iostream>

using namespace std;

long long a[100010];
long long plus[100010];

int main()
{
	int n;
	cin >> n;
	long long ans = 0;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		if (i == 0)
			continue;
		plus[i] = plus[i - 1];
		a[i] += plus[i];
		if (a[i] < a[i - 1])
		{
			int d = a[i - 1] - a[i];
			plus[i] += d;
			ans += d;
			a[i] = a[i - 1];
		}
	}
	cout << ans << endl;
	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

#define REP(i,a) for(int i=0;i<a;i++)
#define REP1(i,a) for(int i=a-1;i>=0;i--)
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORH(i,a,b,h) for(int i=a;i<=b;i+=h)
#define FOR1(i,a,b) for(int i=a;i>=b;i--)
#define FORH1(i,a,b,h) for(int i=a;i>=b;i-=h)
#define MK make_pair
#define sc scanf

#define ull unsigned long long int
#define ll long long int

const ll MOD=1e9+7;
const int N=1e5;

const int V=11*11;
const int E=4*V*V;

int main(){
    int n;
    cin>>n;

    ll a[N];
    REP(i,n) cin>>a[i];

    ll kol=0;
    FOR(i,0,n-2){
        if(a[i]>a[i+1]) kol+=a[i]-a[i+1];
    }

    cout<<kol<<endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

#define REP(i,a) for(int i=0;i<a;i++)
#define REP1(i,a) for(int i=a-1;i>=0;i--)
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORH(i,a,b,h) for(int i=a;i<=b;i+=h)
#define FOR1(i,a,b) for(int i=a;i>=b;i--)
#define FORH1(i,a,b,h) for(int i=a;i>=b;i-=h)
#define MK make_pair
#define sc scanf

#define ull unsigned long long int
#define ll long long int

const ll MOD=1e9+7;
const int N=1e5;

const int V=11*11;
const int E=4*V*V;

int main(){
    int n;
    cin>>n;

    ll a[N];
    REP(i,n) cin>>a[i];

    ll sum=0, kol=0;
    FOR(i,0,n-2){
        if(a[i]>a[i+1]+sum){
            kol+=a[i]-a[i+1]-sum;
            sum=a[i]-a[i+1]-sum;
            a[i+1]=a[i];
        }
    }

    cout<<kol<<endl;
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
	int n,i,a0,a,count=0;
	cin>>n;
	cin>>a0;
	for(i=1;i<n;i++)
	{
		cin>>a;
		if(a<a0) count+=a0-a;
		a0=a;
	}
	cout<<count<<endl;
	return 0;
}


#include<iostream>
using namespace std;
int main()
{
	int n,i,a0,a;
	long long count=0;
	cin>>n;
	cin>>a0;
	for(i=1;i<n;i++)
	{
		cin>>a;
		if(a<a0) count+=a0-a;
		a0=a;
	}
	cout<<count<<endl;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <set>
#include <vector>
#include <cstring>
#include <string>
#include <map>
#include <queue>
#include <stack>
#include <deque>
using namespace std;
#ifndef ONLINE_JUDGE
#define reop(in) reop_func(in)
#else
#define reop
#endif
void reop_func(const char *in) {
    freopen(in, "r", stdin);
}
#define lolo long long

const int MAXN = 100000;
lolo a[MAXN];

int main() {
    reop("in.txt");

	lolo n = 0, tot = 0;
	scanf("%d", &n);
	for (lolo i = 0; i < n; i++) scanf("%d", &a[i]);
	for (lolo i = 1; i < n; i++) if (a[i] + tot < a[i - 1]) tot += a[i - 1] - a[i], a[i] = a[i - 1];
	printf("%d\n", tot);

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <set>
#include <vector>
#include <cstring>
#include <string>
#include <map>
#include <queue>
#include <stack>
#include <deque>
using namespace std;
#ifndef ONLINE_JUDGE
#define reop(in) reop_func(in)
#else
#define reop
#endif
void reop_func(const char *in) {
    freopen(in, "r", stdin);
}
#define lolo long long

const int MAXN = 100000;
lolo a[MAXN];

int main() {
    reop("in.txt");

	lolo n = 0, tot = 0;
	scanf("%d", &n);
	for (lolo i = 0; i < n; i++) scanf("%d", &a[i]);
	for (lolo i = 1; i < n; i++) {
		if (a[i] + tot < a[i - 1])
			tot += a[i - 1] - (a[i] + tot);
		a[i] += tot;
	}
	cout << tot << endl;

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <set>
#include <vector>
#include <cstring>
#include <string>
#include <map>
#include <queue>
#include <stack>
#include <deque>
using namespace std;
#ifndef ONLINE_JUDGE
#define reop(in) reop_func(in)
#else
#define reop
#endif
void reop_func(const char *in) {
    freopen(in, "r", stdin);
}
#define lolo long long

const int MAXN = 100000;
lolo a[MAXN];

int main() {
    reop("in.txt");

	lolo n = 0, tot = 0;
	scanf("%d", &n);
	for (lolo i = 0; i < n; i++) scanf("%d", &a[i]);
	for (lolo i = 1; i < n; i++) if (a[i] + tot < a[i - 1]) tot += a[i - 1] - (a[i] + tot), a[i] = a[i - 1];
	printf("%d\n", tot);

    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <vector>
#include <bitset>

#pragma comment (linker, "/STACK:64000000")

typedef long long ll;

using namespace std;

int main(){
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n, x[100000];
	cin >> n;
	for (int i = 0; i < n - 0.5; i++)
		cin >> x[i];
	int s = 0;
	for (int i = 0; i < n - 1; i++)
		s+= max(0, x[i] - x[i+1]);
	cout << s;

	return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <vector>
#include <bitset>

#pragma comment (linker, "/STACK:64000000")

typedef long long ll;

using namespace std;

int main(){
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n, x[100000];
	cin >> n;
	for (int i = 0; i < n - 0.5; i++)
		cin >> x[i];
	int s = 0;
	for (int i = 0; i < n - 1; i++)
		s+= max(0, x[i+1] - x[i]);
	cout << s;

	return 0;
}


#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <vector>
#include <bitset>

#pragma comment (linker, "/STACK:64000000")

typedef long long ll;

using namespace std;

int main(){
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	ll n, x[100000];
	cin >> n;
	for (int i = 0; i < n - 0.5; i++)
		cin >> x[i];
	ll s = 0;
	for (int i = 0; i < n - 1; i++)
		s+= max(ll(0), x[i] - x[i+1]);
	cout << s;

	return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#define forn(i,n) for(int i = 0; i < (n); ++i)
#define all(x) x.begin(),x.end()

using namespace std;
const int XMAX = 1000000000+7;
const int XMIN = -1;

int main()
{
#ifndef ONLINE_JUDGE
	freopen("ReadMe.txt","r",stdin);
#endif

	int n;
	cin >> n;
	vector < int > mas(n);

	forn(i,n)
		cin >> mas[i];

	int res = 0;
	int xmax = XMIN;
	int cnt = 0;
	forn(i,n)
	{
		if(mas[i] > xmax && xmax == -1)
			xmax = mas[i];
		else if(mas[i] > xmax)
		{
			xmax = mas[i];
			res += cnt;
			cnt = 0;
		}
		else if(xmax-mas[i] > cnt)
			cnt = xmax-mas[i];
	}
	res += cnt;

	cout <<res << endl;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#define forn(i,n) for(int i = 0; i < (n); ++i)
#define all(x) x.begin(),x.end()

using namespace std;
const int XMAX = 1000000000+7;
const int XMIN = -1;

int main()
{
#ifndef ONLINE_JUDGE
	freopen("ReadMe.txt","r",stdin);
#endif

	int n;
	cin >> n;
	vector < int > mas(n);

	forn(i,n)
		cin >> mas[i];

	long long res = 0;
	int xmax = XMIN;
	int cnt = 0;
	forn(i,n)
	{
		if(mas[i] > xmax && xmax == -1)
			xmax = mas[i];
		else if(mas[i] > xmax)
		{
			xmax = mas[i];
			res += cnt;
			cnt = 0;
		}
		else if(xmax-mas[i] > cnt)
			cnt = xmax-mas[i];
	}
	res += cnt;

	cout <<res << endl;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#define forn(i,n) for(int i = 0; i < (n); ++i)
#define all(x) x.begin(),x.end()

using namespace std;

int main()
{
#ifndef ONLINE_JUDGE
	freopen("ReadMe.txt","r",stdin);
#endif

	int n;
	cin >> n;
	vector < int > mas(n);

	forn(i,n)
		cin >> mas[i];

	int res = 0;
	int xmax = -1;

	forn(i,n)
	{
		if(mas[i] > xmax)
			xmax = mas[i];
		else if(mas[i] < xmax && xmax-mas[i] > res)
			res = xmax-mas[i];
	}
	cout << res << endl;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#define forn(i,n) for(int i = 0; i < (n); ++i)
#define all(x) x.begin(),x.end()

using namespace std;
const int XMAX = 1000000000+7;
const int XMIN = -1;

int main()
{
#ifndef ONLINE_JUDGE
	freopen("ReadMe.txt","r",stdin);
#endif

	int n;
	cin >> n;
	vector < int > mas(n);

	forn(i,n)
		cin >> mas[i];

	int res(0);
	int prev = -1;
	forn(i,n)
	{
		if(prev > mas[i])
			res += prev-mas[i];
		prev = mas[i];
	}

	cout << res << endl;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#define forn(i,n) for(int i = 0; i < (n); ++i)
#define all(x) x.begin(),x.end()

using namespace std;

int main()
{
#ifndef ONLINE_JUDGE
	freopen("ReadMe.txt","r",stdin);
#endif

	int n;
	cin >> n;
	vector < int > mas(n);

	forn(i,n)
		cin >> mas[i];

	long long res = 0;
	int xmax = -1;

	forn(i,n)
	{
		if(mas[i] > xmax)
			xmax = mas[i];
		else if(mas[i] < xmax && xmax-mas[i] > res)
			res = xmax-mas[i];
	}
	cout << res << endl;

    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#define forn(i,n) for(int i = 0; i < (n); ++i)
#define all(x) x.begin(),x.end()
typedef long long int64;
using namespace std;
const int XMAX = 1000000000+7;
const int XMIN = -1;

int main()
{
#ifndef ONLINE_JUDGE
	freopen("ReadMe.txt","r",stdin);
#endif

	int n;
	cin >> n;
	vector < int > mas(n);

	forn(i,n)
		cin >> mas[i];

	int64 res(0);
	int prev = -1;
	forn(i,n)
	{
		if(prev > mas[i])
			res += prev-mas[i];
		prev = mas[i];
	}

	cout << res << endl;

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <string.h>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#include <map>
#include <cmath>
#include <set>
using namespace std;
typedef long long ll;

ll n, mas[100010];

int main()
{
    #if !defined(ONLINE_JUDGE)
    freopen("input.txt", "r", stdin);
    freopen("output.txt ", "w", stdout);
    #endif

	cin >> n;
	for (int i = 0; i < n; ++i) {
		cin >> mas[i];
	}

	int ans = 0;
	for (int i = 1; i < n; ++i) {
		if (mas[i] < mas[i-1]) {
			ans += mas[i-1] - mas[i];
		}
	}

	cout << ans << endl;

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <string.h>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#include <map>
#include <cmath>
#include <set>
using namespace std;
typedef long long ll;

int n, mas[100010];

int main()
{
    #if !defined(ONLINE_JUDGE)
    freopen("input.txt", "r", stdin);
    freopen("output.txt ", "w", stdout);
    #endif

	cin >> n;
	for (int i = 0; i < n; ++i) {
		cin >> mas[i];
	}

	int ans = 0;
	for (int i = 1; i < n; ++i) {
		if (mas[i] < mas[i-1]) {
			ans += mas[i-1] - mas[i];
		}
	}

	cout << ans << endl;

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <string.h>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#include <map>
#include <cmath>
#include <set>
using namespace std;
typedef long long ll;

ll n, mas[100010];

int main()
{
    #if !defined(ONLINE_JUDGE)
    freopen("input.txt", "r", stdin);
    freopen("output.txt ", "w", stdout);
    #endif

	cin >> n;
	for (int i = 0; i < n; ++i) {
		cin >> mas[i];
	}

	ll ans = 0;
	for (int i = 1; i < n; ++i) {
		if (mas[i] < mas[i-1]) {
			ans += mas[i-1] - mas[i];
		}
	}

	cout << ans << endl;

    return 0;
}


#include <iostream>
using namespace std;

int main()
{
    int n;
    cin>>n;
    int a[10000]={0};
    for(int i=0;i<n;i++)
        cin>>a[i];

    long long max=0;
    max=0;
    int temp=0;

    int i=0;
    while(i<n-1){
    while((a[i+1]>=a[i])&&(i<n-1))
    {
        i++;
    }
    temp=0;
    while((a[i+1]<a[i])&&(i<n-1))
    {
        temp+=(a[i]-a[i+1]);
        i++;
    }

        max+=temp;
    }

    cout<<max;

    return 0;
}


#include <iostream>
using namespace std;

int main()
{
    int n;
    cin>>n;
    int a[10000]={0};
    for(int i=0;i<n;i++)
        cin>>a[i];

    long long max=0;
    max=0;
    int temp=0;

    int i=0;

    while(i<n-1){
    while((a[i+1]>=a[i])&&(i<n-1))
        i++;

    temp=i;
    while((a[i+1]<a[i])&&(i<n-1))
    {
        //temp+=(a[i]-a[i+1]);
        i++;
    }

        max+=(a[temp]-a[i]);
    }

    cout<<max;

    return 0;
}


#include <iostream>
using namespace std;

int main()
{
    int n;
    cin>>n;
    int a[10000]={0};
    for(int i=0;i<n;i++)
        cin>>a[i];

    int max=0;
    int temp=0;

    /*for(int i=0;i<n-1;i++)
        if(a[i]-a[i+1]>max)
            max=a[i]-a[i+1];*/
    int i=0;
    while(i<n-1){
    while((a[i+1]>=a[i])&&(i<n-1))
    {
        i++;
    }
    temp=0;
    while((a[i+1]<a[i])&&(i<n-1))
    {
        temp+=(a[i]-a[i+1]);
        i++;
    }

        max+=temp;
    }

    cout<<max;

    return 0;
}


#include <iostream>
using namespace std;

int main()
{
    int n;
    cin>>n;
    int a[10000]={0};
    for(int i=0;i<n;i++)
        cin>>a[i];

    int max=0;
    int temp=0;

    /*for(int i=0;i<n-1;i++)
        if(a[i]-a[i+1]>max)
            max=a[i]-a[i+1];*/
    int i=0;
    while(i<n-1){
    while((a[i+1]>=a[i])&&(i<n-1))
    {
        i++;
    }
    temp=0;
    while((a[i+1]<a[i])&&(i<n-1))
    {
        temp+=(a[i]-a[i+1]);
        i++;
    }
    if(temp>max)
        max=temp;
    }

    cout<<max;

    return 0;
}


#include <iostream>
//#include <time.h>
using namespace std;

int main()
{
    int n;
    cin>>n;
    int a[100000]={0};
    //srand((unsigned)time(NULL));
    for(int i=0;i<n;i++)
        cin>>a[i];
        //a[i]=rand()%1000000000;

    __int64 max=0;
    //long long max=0;
    int temp=0;

    int i=0;

    while(i<n-1){
    while(a[i+1]>=a[i])
        i++;

    temp=i;
    if(i<n)
    while((a[i+1]<a[i])&&(i<n-1))
    {
        //temp+=(a[i]-a[i+1]);
        i++;
    }

        max+=(a[temp]-a[i]);
    }

    cout<<max;

    return 0;
}


//#include <iostream>
//#include <vector>
//#include <algorithm>
//#include <map>
//#include <string>
//
//using namespace std;
//
//int main()
//{
//	vector<int>vec;
//	vector<int>res;
//	int temp;
//	while(!cin.eof())
//	{
//		cin>>temp;
//		vec.push_back(temp);
//	}
//	for(int i=0;i<vec.size();i++)
//	{
//		if(vec[i] == 1)
//		{
//			res.push_back(1);
//			continue;
//		}
//		long long num = 11;
//		int cnt = 2;
//		while(num%vec[i])
//		{
//			num*=10;
//			num+=1;
//			cnt++;
//		}
//		res.push_back(cnt);
//	}
//
//	for(int j=0;j<res.size();j++)
//	{
//		if(j+1<res.size())
//			cout<<res[j]<<endl;
//		else cout<<res[j];
//	}
//
//	return 0;
//}

#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <bitset>
#include <map>
#include <algorithm>
//#include <array>
#include <sstream>

using namespace std;
string intToString(int num)
{
	ostringstream ss;
	ss<<num;
	return ss.str();
}
/*
int main()
{
	string str1,str2;
	cin>>str1>>str2;
	int len = str2.length();
	for(int i=0,j=len-1;i<=j;i++,j--)
	{
		char c = str2[i];
		str2[i] = str2[j];
		str2[j] = c;
	}
	if(str1 == str2)
		cout<<"YES"<<endl;
	else cout<<"NO"<<endl;
	return 0;
}
*/
/*
int main()
{
	int n;
	cin>>n;
	bool arr[3001];
	for(int i=1;i<3001;i++)
		arr[i] = false;
	for(int i=0;i<n;i++)
	{
		int temp;
		cin>>temp;
		arr[temp] = true;
	}
	for(int j=1;j<3001;j++)
	{
		if(!arr[j])
		{
			cout<<j<<endl;
			return 0;
		}
	}
	cout<<3001<<endl;

	return 0;
}
*/

/*
int main()
{
	int a,b;
	unsigned long long	cnt = 0;
	cin>>a>>b;
	for(int i=a;i>1;i--)
	{
		for(int j=b;j>1;j--)
		{
			if(!(j&1 || i&1))
				cnt++;
		}
	}
	cout<<cnt<<endl;

	return 0;
}
*/
/*
int main()
{
	int n=0;
	cin>>n;
	vector<string>vec;
	for(int i=0;i<n;i++)
	{
		string str;
		cin>>str;
		vec.push_back(str);
	}

	int cnt = 0, len = vec[0].length();

	for(int j=0;j<len;j++)
	{
		char c = vec[0][j];
		bool add = true;
		for(int i=1;i<n;i++)
		{
			if(c != vec[i][j])
			{add = false; break;}
		}
		if(add)
			cnt++;
		else break;
	}
	cout<<cnt<<endl;
	return 0;
}
*/
/*
int main()
{
	int n = 0, k = 0;
	cin>>n>>k;
	k--;
	vector<int>vec;
	for(int i=0;i<n;i++)
	{
		int t = 0;
		cin>>t;
		vec.push_back(t);
	}
	int start = n-1, end = n-1;
	for(int i=n-1;i>0;i--)
	{
		if(vec[i] == vec[i-1])
			start--;
		else break;
	}
	if(start == end)
	{
		if(k==end)
			cout<<n-1<<endl;
		else
			cout<<-1<<endl;
		return 0;
	}
	else
	{
		if(k >= start)
			cout<<n-(end-start+1)<<endl;
		else
			cout<<-1<<endl;
		return 0;
	}

	return 0;
}
*/
/*
int main()
{
	int n = 0;
	cin>>n;
	int top = 0;
	cin>>top;
	vector<pair<int,int>>vec;
	for(int i=0;i<n;i++)
	{
		int t1 = 0, t2 = 0;
		cin>>t1>>t2;
		vec.push_back(pair<int,int>(t1,t2));
	}
	int bottom = 7-top;
	for(int i=0;i<vec.size();i++)
	{
		if(vec[i].first == bottom|| vec[i].second == bottom || vec[i].first == top || vec[i].second == top)
		{
			cout<<"NO"<<endl;
			return 0;
		}
	}
	cout<<"YES"<<endl;

	return 0;
}
*/
/*
int main()
{
	int n=0;
	cin>>n;
	cout<<(n+(n>>1))<<endl;
	return 0;
}
*/
int main()
{
	int n = 0;
	cin>>n;
	vector<int>vec;
	for(int i=0;i<n;i++)
	{
		int j = 0;
		cin>>j;
		vec.push_back(j);
	}
	long long cnt = 0;
	for(int i=0;i<n-1;i++)
	{
		if(vec[i] > vec[i+1])
		{
			cnt += abs(vec[i]-vec[i+1]);
		}
	}
	cout<<cnt<<endl;

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

bool isSorted(vector <int> vec)
{
	for(int i=0;i<vec.size()-1;i++)
	{
		if(vec[i] > vec[i+1])
			return false;
	}
	return true;
}

int main()
{
	int n;
	cin>>n;
	vector<int>vec;
	for(int i=0;i<n;i++)
	{
		int temp=0;
		cin>>temp;
		vec.push_back(temp);
	}

	int cnt=0;
	while(!isSorted(vec))
	{
		bool temp = false;
		for(int i=0;i<vec.size()-1;i++)
		{
			if(vec[i]>vec[i+1])
			{
				vec[i+1]++;
				temp = true;
			}
			else
			{
				if(temp)
					break;
			}
		}
		cnt++;
	}
	cout<<cnt<<endl;

	return 0;
}


//#include <iostream>
//#include <vector>
//#include <algorithm>
//#include <map>
//#include <string>
//
//using namespace std;
//
//int main()
//{
//	vector<int>vec;
//	vector<int>res;
//	int temp;
//	while(!cin.eof())
//	{
//		cin>>temp;
//		vec.push_back(temp);
//	}
//	for(int i=0;i<vec.size();i++)
//	{
//		if(vec[i] == 1)
//		{
//			res.push_back(1);
//			continue;
//		}
//		long long num = 11;
//		int cnt = 2;
//		while(num%vec[i])
//		{
//			num*=10;
//			num+=1;
//			cnt++;
//		}
//		res.push_back(cnt);
//	}
//
//	for(int j=0;j<res.size();j++)
//	{
//		if(j+1<res.size())
//			cout<<res[j]<<endl;
//		else cout<<res[j];
//	}
//
//	return 0;
//}

#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <bitset>
#include <map>
#include <algorithm>
//#include <array>
#include <sstream>

using namespace std;
string intToString(int num)
{
	ostringstream ss;
	ss<<num;
	return ss.str();
}
/*
int main()
{
	string str1,str2;
	cin>>str1>>str2;
	int len = str2.length();
	for(int i=0,j=len-1;i<=j;i++,j--)
	{
		char c = str2[i];
		str2[i] = str2[j];
		str2[j] = c;
	}
	if(str1 == str2)
		cout<<"YES"<<endl;
	else cout<<"NO"<<endl;
	return 0;
}
*/
/*
int main()
{
	int n;
	cin>>n;
	bool arr[3001];
	for(int i=1;i<3001;i++)
		arr[i] = false;
	for(int i=0;i<n;i++)
	{
		int temp;
		cin>>temp;
		arr[temp] = true;
	}
	for(int j=1;j<3001;j++)
	{
		if(!arr[j])
		{
			cout<<j<<endl;
			return 0;
		}
	}
	cout<<3001<<endl;

	return 0;
}
*/

/*
int main()
{
	int a,b;
	unsigned long long	cnt = 0;
	cin>>a>>b;
	for(int i=a;i>1;i--)
	{
		for(int j=b;j>1;j--)
		{
			if(!(j&1 || i&1))
				cnt++;
		}
	}
	cout<<cnt<<endl;

	return 0;
}
*/
/*
int main()
{
	int n=0;
	cin>>n;
	vector<string>vec;
	for(int i=0;i<n;i++)
	{
		string str;
		cin>>str;
		vec.push_back(str);
	}

	int cnt = 0, len = vec[0].length();

	for(int j=0;j<len;j++)
	{
		char c = vec[0][j];
		bool add = true;
		for(int i=1;i<n;i++)
		{
			if(c != vec[i][j])
			{add = false; break;}
		}
		if(add)
			cnt++;
		else break;
	}
	cout<<cnt<<endl;
	return 0;
}
*/
/*
int main()
{
	int n = 0, k = 0;
	cin>>n>>k;
	k--;
	vector<int>vec;
	for(int i=0;i<n;i++)
	{
		int t = 0;
		cin>>t;
		vec.push_back(t);
	}
	int start = n-1, end = n-1;
	for(int i=n-1;i>0;i--)
	{
		if(vec[i] == vec[i-1])
			start--;
		else break;
	}
	if(start == end)
	{
		if(k==end)
			cout<<n-1<<endl;
		else
			cout<<-1<<endl;
		return 0;
	}
	else
	{
		if(k >= start)
			cout<<n-(end-start+1)<<endl;
		else
			cout<<-1<<endl;
		return 0;
	}

	return 0;
}
*/
/*
int main()
{
	int n = 0;
	cin>>n;
	int top = 0;
	cin>>top;
	vector<pair<int,int>>vec;
	for(int i=0;i<n;i++)
	{
		int t1 = 0, t2 = 0;
		cin>>t1>>t2;
		vec.push_back(pair<int,int>(t1,t2));
	}
	int bottom = 7-top;
	for(int i=0;i<vec.size();i++)
	{
		if(vec[i].first == bottom|| vec[i].second == bottom || vec[i].first == top || vec[i].second == top)
		{
			cout<<"NO"<<endl;
			return 0;
		}
	}
	cout<<"YES"<<endl;

	return 0;
}
*/
/*
int main()
{
	int n=0;
	cin>>n;
	cout<<(n+(n>>1))<<endl;
	return 0;
}
*/
int main()
{
	int n = 0;
	cin>>n;
	vector<int>vec;
	for(int i=0;i<n;i++)
	{
		int j = 0;
		cin>>j;
		vec.push_back(j);
	}
	int cnt = 0;
	for(int i=0;i<n-1;i++)
	{
		if(vec[i] > vec[i+1])
		{
			cnt += abs(vec[i]-vec[i+1]);
		}
	}
	cout<<cnt<<endl;

	return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;

int main(){
    int n,i,prev,next;

    long long res=0;
    cin>>n;
    cin>>prev;
    for(i=1;i<n;++i)
    {
    cin>>next;
    if(prev>next)res+=prev-next;

    prev=next;
    }

    cout<<res;

    }


#include<iostream>
#include<algorithm>
using namespace std;

int main(){
    int n,i,*m=new int [100001],*m1=new int [100001],min=0;
    bool v=false;
    cin>>n;
    for(i=0;i<n;++i)
    {
    cin>>m[i];
    m1[i]=m[i];
    }

    sort(m1,m1+n);

    for(i=0;i<n;i++){
                     int a=abs(m[i]-m1[i]);
                     if(a>min)min=a;
                     }

    cout<<min;

    }


#include<iostream>
#include<algorithm>
using namespace std;

int main(){
    int n,i,prev,res=0,next;
    bool v=false;
    cin>>n;
    cin>>prev;
    for(i=1;i<n;++i)
    {
    cin>>next;
    if(prev>next)res+=prev-next;

    prev=next;
    }

    cout<<res;

    }


#include <cstring>
#include <string.h>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <vector>
#include <set>
#include <complex>
#include <list>

using namespace std;

#define pb push_back
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz size()
#define rep(i,m) for(int i=0;i<(int)(m);i++)
#define rep2(i,n,m) for(int i=n;i<(int)(m);i++)
#define For(it,c) for(__typeof(c.begin()) it=c.begin();it!=c.end();++it)
#define mem(a,b) memset(a,b,sizeof(a))
#define mp make_pair
#define dot(a,b) ((conj(a)*(b)).X)
#define X real()
#define Y imag()
#define length(V) (hypot((V).X,(V).Y))
#define vect(a,b) ((b)-(a))
#define cross(a,b) ((conj(a)*(b)).imag())
#define normalize(v) ((v)/length(v))
#define rotate(p,about,theta) ((p-about)*exp(point(0,theta))+about)
#define pointEqu(a,b) (comp(a.X,b.X)==0 && comp(a.Y,b.Y)==0)
#define sorta(v) sort(all(v))
#define sortid(v) sort(all(v), greater<int>())
#define sortdd(v) sort(all(v), greater<double>())

typedef stringstream ss;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<string> vs;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<vector<int> > vii;
typedef long long ll;
typedef long double ld;
typedef complex<double> point;
typedef pair<point, point> segment;
typedef pair<double, point> circle;
typedef vector<point> polygon;;

const int oo = (int) 1e9;
const double PI = 2 * acos(0.0);
const double eps = 1e-9;

template <class T> void out( T a, T b ) { bool first = true; for( T i = a; i != b; ++ i ) { if( !first ) printf( " " ); first = false; cout << * i; } printf( "\n" ); }
template <class T> void outl( T a, T b ) { for( T i = a; i != b; ++ i ) { cout << * i << "\n"; } }
template <class T> void outc(T a, T b) { for (T i = a; i  != b; ++i) {  cout << * i << ((i+1) != b ? ", " : ""); } }

int ni() { int a; scanf( "%d", &a ); return a; }
double nf() { double a; scanf( "%lf", &a ); return a; }
long long nll() { long long a; scanf( "%lld", &a ); return a; }
char sbuf[100005]; string ns() { scanf( "%s", sbuf ); return sbuf; }
string nline() {gets(sbuf); return sbuf;}

int main() {

	// ! get inputs !
#ifndef ONLINE_JUDGE
	freopen("d:\\gcj\\input.in","rt",stdin);
//	freopen("d:\\gcj\\output.out","wt",stdout);

#endif

int n = ni ();

long long cnt = 0;
long long a, b;
rep(ii, n) {
	cin >> b;
	if (ii >= 1 && b < a) {
		cnt += a - b;
	}
	a = b;
}
cout << cnt << endl;
 #ifndef ONLINE_JUDGE
                printf("\nTime used = %.3lf\n", (double)clock()/CLOCKS_PER_SEC);
                printf("****** END ******\n");
 #endif

	return 0;
}


#include <cstring>
#include <string.h>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <vector>
#include <set>
#include <complex>
#include <list>

using namespace std;

#define pb push_back
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz size()
#define rep(i,m) for(int i=0;i<(int)(m);i++)
#define rep2(i,n,m) for(int i=n;i<(int)(m);i++)
#define For(it,c) for(__typeof(c.begin()) it=c.begin();it!=c.end();++it)
#define mem(a,b) memset(a,b,sizeof(a))
#define mp make_pair
#define dot(a,b) ((conj(a)*(b)).X)
#define X real()
#define Y imag()
#define length(V) (hypot((V).X,(V).Y))
#define vect(a,b) ((b)-(a))
#define cross(a,b) ((conj(a)*(b)).imag())
#define normalize(v) ((v)/length(v))
#define rotate(p,about,theta) ((p-about)*exp(point(0,theta))+about)
#define pointEqu(a,b) (comp(a.X,b.X)==0 && comp(a.Y,b.Y)==0)
#define sorta(v) sort(all(v))
#define sortid(v) sort(all(v), greater<int>())
#define sortdd(v) sort(all(v), greater<double>())

typedef stringstream ss;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<string> vs;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<vector<int> > vii;
typedef long long ll;
typedef long double ld;
typedef complex<double> point;
typedef pair<point, point> segment;
typedef pair<double, point> circle;
typedef vector<point> polygon;;

const int oo = (int) 1e9;
const double PI = 2 * acos(0.0);
const double eps = 1e-9;

template <class T> void out( T a, T b ) { bool first = true; for( T i = a; i != b; ++ i ) { if( !first ) printf( " " ); first = false; cout << * i; } printf( "\n" ); }
template <class T> void outl( T a, T b ) { for( T i = a; i != b; ++ i ) { cout << * i << "\n"; } }
template <class T> void outc(T a, T b) { for (T i = a; i  != b; ++i) {  cout << * i << ((i+1) != b ? ", " : ""); } }

int ni() { int a; scanf( "%d", &a ); return a; }
double nf() { double a; scanf( "%lf", &a ); return a; }
long long nll() { long long a; scanf( "%lld", &a ); return a; }
char sbuf[100005]; string ns() { scanf( "%s", sbuf ); return sbuf; }
string nline() {gets(sbuf); return sbuf;}

int main() {

	// ! get inputs !
#ifndef ONLINE_JUDGE
	freopen("d:\\gcj\\input.in","rt",stdin);
//	freopen("d:\\gcj\\output.out","wt",stdout);

#endif

int n = ni ();
vector<long long> a;
a.resize(n);
rep(ii, n)
	cin >> a[ii];

long long cnt = 0;
int cur = 0;
while (cur < n-1) {
	if (a[cur+1] >= a[cur]) {
		cur++;
	} else {
		// choose interval
		int idx = cur +1;
		while (idx < n && a[idx] < a[cur]) idx++;
		int step = a[cur]  - a[cur+1];
		rep2(ij, cur+1, idx)
			a[ij] += step;
		cnt += step;
		cur++;
	}
}
cout << cnt << endl;
 #ifndef ONLINE_JUDGE
                printf("\nTime used = %.3lf\n", (double)clock()/CLOCKS_PER_SEC);
                printf("****** END ******\n");
 #endif

	return 0;
}


#include <cstring>
#include <string.h>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <vector>
#include <set>
#include <complex>
#include <list>

using namespace std;

#define pb push_back
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz size()
#define rep(i,m) for(int i=0;i<(int)(m);i++)
#define rep2(i,n,m) for(int i=n;i<(int)(m);i++)
#define For(it,c) for(__typeof(c.begin()) it=c.begin();it!=c.end();++it)
#define mem(a,b) memset(a,b,sizeof(a))
#define mp make_pair
#define dot(a,b) ((conj(a)*(b)).X)
#define X real()
#define Y imag()
#define length(V) (hypot((V).X,(V).Y))
#define vect(a,b) ((b)-(a))
#define cross(a,b) ((conj(a)*(b)).imag())
#define normalize(v) ((v)/length(v))
#define rotate(p,about,theta) ((p-about)*exp(point(0,theta))+about)
#define pointEqu(a,b) (comp(a.X,b.X)==0 && comp(a.Y,b.Y)==0)
#define sorta(v) sort(all(v))
#define sortid(v) sort(all(v), greater<int>())
#define sortdd(v) sort(all(v), greater<double>())

typedef stringstream ss;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<string> vs;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<vector<int> > vii;
typedef long long ll;
typedef long double ld;
typedef complex<double> point;
typedef pair<point, point> segment;
typedef pair<double, point> circle;
typedef vector<point> polygon;;

const int oo = (int) 1e9;
const double PI = 2 * acos(0.0);
const double eps = 1e-9;

template <class T> void out( T a, T b ) { bool first = true; for( T i = a; i != b; ++ i ) { if( !first ) printf( " " ); first = false; cout << * i; } printf( "\n" ); }
template <class T> void outl( T a, T b ) { for( T i = a; i != b; ++ i ) { cout << * i << "\n"; } }
template <class T> void outc(T a, T b) { for (T i = a; i  != b; ++i) {  cout << * i << ((i+1) != b ? ", " : ""); } }

int ni() { int a; scanf( "%d", &a ); return a; }
double nf() { double a; scanf( "%lf", &a ); return a; }
long long nll() { long long a; scanf( "%lld", &a ); return a; }
char sbuf[100005]; string ns() { scanf( "%s", sbuf ); return sbuf; }
string nline() {gets(sbuf); return sbuf;}

int main() {

	// ! get inputs !
#ifndef ONLINE_JUDGE
	freopen("d:\\gcj\\input.in","rt",stdin);
//	freopen("d:\\gcj\\output.out","wt",stdout);

#endif

int n = ni ();
vi a;
a.resize(n);
rep(ii, n)
	cin >> a[ii];

int cnt = 0;
int cur = 0;
while (cur < n-1) {
	if (a[cur+1] >= a[cur]) {
		cur++;
	} else {
		// choose interval
		int idx = cur +1;
		while (idx < n && a[idx] < a[cur]) idx++;
		int step = a[cur]  - a[cur+1];
		rep2(ij, cur+1, idx)
			a[ij] += step;
		cnt += step;
		cur++;
	}
}
cout << cnt << endl;
 #ifndef ONLINE_JUDGE
                printf("\nTime used = %.3lf\n", (double)clock()/CLOCKS_PER_SEC);
                printf("****** END ******\n");
 #endif

	return 0;
}


#include<stdio.h>
#include<iostream>
#include<queue>

using namespace std;
#define MAX 100000
#define Int __int64
#define In 2000000000
Int A[MAX+1]={-In};

int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int n,i,j;Int ans=0;
	scanf("%d",&n);
	for(i=0;i<n;i++)
		scanf("%I64d",&A[i]);
	for(i=1;i<n;i++)
		if(A[i]<A[i-1])
			ans+=A[i-1]-A[i];
	printf("%I64d\n",ans);
	return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 100000
typedef long long LL;
int a[MAXN +10];
int b,q,l,m;
#define ABS(x)  ( (x)>0?(x):-(x) )
bool in(int k)
{
	if(a[lower_bound(a+1,a+1+m,k) - a] == k)
		return true;
	return false;
}
int main()
{
	scanf("%d%d%d%d",&b,&q,&l,&m);
	for(int i = 1;i <= m;i++)
		scanf("%d",&a[i]);
	sort(a+1,a+1+m);
	LL nb;
	int sum = 0;
	if(q == -1)
	{
		q = 1;
		nb = -b;
		while(1)
		{
			if(ABS(nb) > l)
				break;
			if(!in(nb))
				sum++;
			nb *= q;
			if(nb * q == nb)
			{
				printf("inf");
				return 0;
			}
		}
	}
	nb = b;
	while(1)
	{
		if(ABS(nb) > l)
			break;
		if(!in(nb))
			sum++;
		nb *= q;
		if(nb * q == nb)
		{
			printf("inf");
			return 0;
		}
	}
	printf("%d",sum);
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 100000
typedef long long LL;
int a[MAXN +10];
int b,q,l,m;
#define ABS(x)  ( (x)>0?(x):-(x) )
bool in(int k)
{
	int num = lower_bound(a+1,a+1+m,k) - a;
	if(num <= m && a[num] == k)
		return true;
	return false;
}
int main()
{
	scanf("%d%d%d%d",&b,&q,&l,&m);
	for(int i = 1;i <= m;i++)
		scanf("%d",&a[i]);
	sort(a+1,a+1+m);
	LL nb = b;
	int sum = 0,times = 0;
	while(1)
	{
		times++;
		if(ABS(nb) > l)
			break;
		if(!in(nb))
			sum++;
		nb *= q;
		if(times > 50)
		{
			if(sum > 10)
				printf("inf");
			else
				printf("%d",sum);
			return 0;
		}
	}
	printf("%d",sum);
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 100000
typedef long long LL;
int a[MAXN +10];
int b,q,l,m;
bool in(int k)
{
	if(a[lower_bound(a+1,a+1+m,k) - a] == k)
		return true;
	return false;
}
int main()
{
	scanf("%d%d%d%d",&b,&q,&l,&m);
	for(int i = 1;i <= m;i++)
		scanf("%d",&a[i]);
	sort(a+1,a+1+m);
	if(b == 0)
	{
		if(in(b))
			printf("0");
		else
			printf("inf");
		return 0;
	}
	else if(q == 0)
	{
		if(!in(0))
			printf("inf");
		else if(in(b))
			printf("0");
		else
			printf("1");
			return 0;
	}
	else if(q == 1)
	{
			if(!in(b))
				printf("inf");
			else
				printf("0");
			return 0;
	}
	else if(q == -1)
	{
			if(!in(b) || !in(-1*b))
				printf("inf");
			else
				printf("0");
			return 0;
	}
	int nb = b,sum = 0;
	while(1)
	{
		if(abs(nb) > l)
			break;
		if(!in(nb))
			sum++;
		nb *= q;
	}
	printf("%d",sum);
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 100000
typedef long long LL;
int a[MAXN +10];
int b,q,l,m;
#define ABS(x)  ( (x)>0?(x):-(x) )
bool in(int k)
{
	if(a[lower_bound(a+1,a+1+m,k) - a] == k)
		return true;
	return false;
}
int main()
{
	scanf("%d%d%d%d",&b,&q,&l,&m);
	for(int i = 1;i <= m;i++)
		scanf("%d",&a[i]);
	sort(a+1,a+1+m);
	LL nb = b;
	int sum = 0,times = 0;
	while(1)
	{
		times++;
		if(ABS(nb) > l)
			break;
		if(!in(nb))
			sum++;
		nb *= q;
		if(times > 50)
		{
			if(sum > 0)
				printf("inf");
			else
				printf("0");
			return 0;
		}
	}
	printf("%d",sum);
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 100000
typedef long long LL;
int a[MAXN +10];
int b,q,l,m;
#define ABS(x)  ( (x)>0?(x):-(x) )
bool in(int k)
{
	int num = lower_bound(a+1,a+1+m,k) - a;
	if(num <= m && a[num] == k)
		return true;
	return false;
}
int main()
{
	scanf("%d%d%d%d",&b,&q,&l,&m);
	for(int i = 1;i <= m;i++)
		scanf("%d",&a[i]);
	sort(a+1,a+1+m);
	if(b == 0)
	{
		if(in(b))
			printf("0");
		else
			printf("inf");
		return 0;
	}
	else if(q == 0)
	{
		if(ABS(b) > l)
			printf("0");
		else if(!in(0))
			printf("inf");
		else if(in(b))
			printf("0");
		else
			printf("1");
			return 0;
	}
	else if(q == 1)
	{
			if(in(b) || ABS(b) > l)
				printf("0");
			else
				printf("inf");
			return 0;
	}
	else if(q == -1)
	{
			if(ABS(b) > l || (in(b) && in(-b)) )
				printf("0");
			else
				printf("inf");
			return 0;
	}
	LL nb = b;
	int sum = 0;
	while(1)
	{
		if(ABS(nb) > l)
			break;
		if(!in(nb))
			sum++;
		nb *= q;
	}
	printf("%d",sum);
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 100000
typedef long long LL;
int a[MAXN +10];
int b,q,l,m;
#define ABS(x)  ( (x)>0?(x):-(x) )
bool in(int k)
{
	if(a[lower_bound(a+1,a+1+m,k) - a] == k)
		return true;
	return false;
}
int main()
{
	scanf("%d%d%d%d",&b,&q,&l,&m);
	for(int i = 1;i <= m;i++)
		scanf("%d",&a[i]);
	sort(a+1,a+1+m);
	if(b == 0)
	{
		if(in(b))
			printf("0");
		else
			printf("inf");
		return 0;
	}
	else if(q == 0)
	{
		if(ABS(b) > l)
			printf("0");
		else if(!in(0))
			printf("inf");
		else if(in(b))
			printf("0");
		else
			printf("1");
			return 0;
	}
	else if(q == 1)
	{
			if(in(b) || ABS(b) > l)
				printf("0");
			else
				printf("inf");
			return 0;
	}
	else if(q == -1)
	{
			if(ABS(b) > l || (in(b) && in(-b)) )
				printf("0");
			else
				printf("inf");
			return 0;
	}
	LL nb = b;
	int sum = 0;
	while(1)
	{
		if(ABS(nb) > l)
			break;
		if(!in(nb))
			sum++;
		nb *= q;
	}
	printf("%d",sum);
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 100000
typedef long long LL;
int a[MAXN +10];
int b,q,l,m;
#define ABS(x)  ( (x)>0?(x):-(x) )
bool in(int k)
{
	if(a[lower_bound(a+1,a+1+m,k) - a] == k)
		return true;
	return false;
}
int main()
{
	scanf("%d%d%d%d",&b,&q,&l,&m);
	for(int i = 1;i <= m;i++)
		scanf("%d",&a[i]);
	sort(a+1,a+1+m);
	LL nb = b;
	int sum = 0,times = 0;
	while(1)
	{
		times++;
		if(ABS(nb) > l)
			break;
		if(!in(nb))
			sum++;
		nb *= q;
		if(times > 50)
		{
			if(sum > 10)
				printf("inf");
			else
				printf("%d",sum);
			return 0;
		}
	}
	printf("%d",sum);
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pp pair<int,int>
#define mp make_pair
#define pb push_back
#define S second
#define F first

#define sf(x) scanf("%d",&x);
#define sff(x,y) scanf("%d%d",&x,&y);
 //string s1="sura";
const int MAX=2e5+100;
const int MOD=1e9+7;
//int freq[1000001];
map<int,int>m1;
int main()
{
   int b1,q,l,m,a;
   int arr[100003];
   sff(b1,q);
   sff(l,m);
   for(int i=0;i<m;++i)
   {
       sf(arr[i]);
       m1[arr[i]]=1;

   }
   int counta=0;
   if(((q==1&&m1[b1]!=1&&(abs(b1)<=l))||(q==0&&m1[0]!=1)||(q==-1&&(m1[b1]!=1||m1[-b1]!=1)&&(abs(b1)<=l)))&&(abs(b1)<=l))
   {
       printf("inf\n");
   }
   else if(((q==1)&&(m1[b1]==1)||(q==0&&m1[b1]==1&&m1[0]==1)||(q==-1&&m1[b1]==1&&m1[-b1]==1))||abs(b1)>l)
   {
       printf("0\n");
   }
   else if(q==0&&m1[b1]!=1&&abs(b1)<=l&&m1[0]==1)
   {
       printf("1\n");

   }
   else if(b1==0&&m1[b1]==1)
   {
       printf("0\n");
   }
   else if(b1==0&&m1[b1]!=1)
   {
       printf("inf\n");
   }

   else if(abs(q)>1){
       // for(int i=0;i<)
       int dd;
       int ii,bad=0;
       for(int i=0;i<m;++i)
       {
          // printf("abs(arr[i]) %d ",abs(arr[i]));
          // long long a=arr[i];
           //printf("abs %d",abs(a));
          // if(arr[i]>0)

           if(abs(arr[i])<=l)
         {// printf("\n");

         //  printf(" arr[i] = %d ",arr[i]);
            ii=1;
           dd=arr[i]/b1;
          // printf("dd= %d ",dd);
           if(arr[i]%b1==0)
           {
               for(int j=0;;++j)
               {  // printf(" ii = %d  ",ii);
                    if(ii==dd)
                   { //printf(" bad word %d ",arr[i]);
                       bad++;break;
                   }
                   ii*=q;

                   if(abs(ii)>abs(dd))
                   {
                       break;
                   }
               }
           }
       }
       }

   while(1)
   { //printf("%d ",b1);

      if(abs(b1)<=l)
      {
          counta++;//b1*=q;
      }b1*=q;
      if(abs(b1)>l)break;
   }

   printf("%d\n",counta-bad);
   }

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pp pair<int,int>
#define mp make_pair
#define pb push_back
#define S second
#define F first

#define sf(x) scanf("%d",&x);
#define sff(x,y) scanf("%d%d",&x,&y);
 //string s1="sura";
const int MAX=2e5+100;
const int MOD=1e9+7;
//int freq[1000001];
map<int,int>m1;
int main()
{
   int b1,q,l,m,a,arr[100003];
   sff(b1,q);
   sff(l,m);
   for(int i=0;i<m;++i)
   {
       sf(arr[i]);
       m1[arr[i]]=1;

   }
   int counta=0;
   if((q==0||q==1)&&(m1[b1]!=1)&&(m1[0]!=1))
   {
       printf("inf\n");
   }
   else if((q==1)&&(m1[b1]==1))
   {
       printf("0\n");
   }
   else if(q==0&&m1[0]==1&&m1[b1]!=1)
   {
       printf("1\n");
   }
   else if(m1[b1]==1&&q==0)
   {
       printf("inf\n");
   }
   else{
       // for(int i=0;i<)
       double dd;
       int ii=q,bad=0;
       for(int i=0;i<m;++i)
       { ii=q;
           dd=(double)arr[i]/b1;
           if(floor(dd)==dd)
           {
               for(int j=0;j<=30;++j)
               {
                   ii*=q;
                   if(ii==dd)
                   {
                       bad++;break;
                   }
                   if(ii>arr[i])
                   {
                       break;
                   }
               }
           }
       }

   while(1)
   { //printf("%d ",b1);

      if(abs(b1)<=l)
      {
          counta++;//b1*=q;
      }b1*=q;
      if(abs(b1)>l)break;
   }

   printf("%d\n",counta-bad);
   }

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pp pair<int,int>
#define mp make_pair
#define pb push_back
#define S second
#define F first

#define sf(x) scanf("%d",&x);
#define sff(x,y) scanf("%d%d",&x,&y);
 //string s1="sura";
const int MAX=2e5+100;
const int MOD=1e9+7;
//int freq[1000001];
map<int,int>m1;
int main()
{
   int b1,q,l,m,a,arr[100003];
   sff(b1,q);
   sff(l,m);
   for(int i=0;i<m;++i)
   {
       sf(arr[i]);
       m1[arr[i]]=1;

   }
   int counta=0;
   if((q==1&&m1[b1]!=1&&(abs(b1)<=l))||(q==0&&m1[0]!=1)||(q==-1&&m1[b1]!=1&&m1[-b1]!=1&&(abs(b1)<=l)))
   {
       printf("inf\n");
   }
   else if((q==1)&&(m1[b1]==1)||(q==0&&m1[b1]==1&&m1[0]==1)||(q==-1&&m1[b1]==1&&m1[-b1]==1))
   {
       printf("0\n");
   }
   else if(q==0&&m1[b1]!=1&&abs(b1)<=l&&m1[0]==1)
   {
       printf("1\n");

   }

   else if(abs(q)>1){
       // for(int i=0;i<)
       double dd;
       int ii=q,bad=0;
       for(int i=0;i<m;++i)
       { if(arr[i]<=l)
       {

            ii=1;
           dd=(double)arr[i]/b1;
           if(floor(dd)==dd)
           {
               for(int j=0;j<=30;++j)
               {  if(ii==dd)
                   {// printf("%d ",arr[i]);
                       bad++;break;
                   }
                   ii*=q;

                   if(ii>arr[i])
                   {
                       break;
                   }
               }
           }
       }
       }

   while(1)
   { //printf("%d ",b1);

      if(abs(b1)<=l)
      {
          counta++;//b1*=q;
      }b1*=q;
      if(abs(b1)>l)break;
   }

   printf("%d\n",counta-bad);
   }

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pp pair<int,int>
#define mp make_pair
#define pb push_back
#define S second
#define F first

#define sf(x) scanf("%d",&x);
#define sff(x,y) scanf("%d%d",&x,&y);
 //string s1="sura";
const int MAX=2e5+100;
const int MOD=1e9+7;
//int freq[1000001];
map<int,int>m1;
int main()
{
   int b1,q,l,m,a,arr[100003];
   sff(b1,q);
   sff(l,m);
   for(int i=0;i<m;++i)
   {
       sf(arr[i]);
       m1[arr[i]]=1;

   }
   int counta=0;
   if((q==0||q==1)&&(m1[b1]!=1)&&(m1[0]!=1))
   {
       printf("inf\n");
   }
   else if((q==1)&&(m1[b1]==1))
   {
       printf("0\n");
   }
   else if(q==0&&m1[0]==1&&m1[b1]!=1)
   {
       printf("1\n");
   }
   else if(m1[b1]==1&&q==0)
   {
       printf("inf\n");
   }
   else{
       // for(int i=0;i<)
       double dd;
       int ii=q,bad=0;
       for(int i=0;i<m;++i)
       { if(arr[i]<=l)
       {

            ii=1;
           dd=(double)arr[i]/b1;
           if(floor(dd)==dd)
           {
               for(int j=0;j<=30;++j)
               {  if(ii==dd)
                   {// printf("%d ",arr[i]);
                       bad++;break;
                   }
                   ii*=q;

                   if(ii>arr[i])
                   {
                       break;
                   }
               }
           }
       }
       }

   while(1)
   { //printf("%d ",b1);

      if(abs(b1)<=l)
      {
          counta++;//b1*=q;
      }b1*=q;
      if(abs(b1)>l)break;
   }

   printf("%d\n",counta-bad);
   }

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pp pair<int,int>
#define mp make_pair
#define pb push_back
#define S second
#define F first

#define sf(x) scanf("%d",&x);
#define sff(x,y) scanf("%d%d",&x,&y);
 //string s1="sura";
const int MAX=2e5+100;
const int MOD=1e9+7;
//int freq[1000001];
map<int,int>m1;
int main()
{
   int b1,q,l,m,a;
   int arr[100003];
   sff(b1,q);
   sff(l,m);
   for(int i=0;i<m;++i)
   {
       sf(arr[i]);
       m1[arr[i]]=1;

   }
   int counta=0;
   if(((q==1&&m1[b1]!=1&&(abs(b1)<=l))||(q==0&&m1[0]!=1)||(q==-1&&(m1[b1]!=1||m1[-b1]!=1)&&(abs(b1)<=l)))&&(abs(b1)<=l))
   {
       printf("inf\n");
   }
   else if(((q==1)&&(m1[b1]==1)||(q==0&&m1[b1]==1&&m1[0]==1)||(q==-1&&m1[b1]==1&&m1[-b1]==1))||abs(b1)>l)
   {
       printf("0\n");
   }
   else if(q==0&&m1[b1]!=1&&abs(b1)<=l&&m1[0]==1)
   {
       printf("1\n");

   }
   else if(b1==0&&m1[b1]==1)
   {
       printf("0\n");
   }
   else if(b1==0&&m1[b1]!=1)
   {
       printf("inf\n");
   }

   else if(abs(q)>1){
       // for(int i=0;i<)
       int dd;
       int ii,bad=0;
       for(int i=0;i<m;++i)
       {
          // printf("abs(arr[i]) %d ",abs(arr[i]));
          // long long a=arr[i];
           //printf("abs %d",abs(a));
          // if(arr[i]>0)

           if(abs(arr[i])<=l)
         {// printf("\n");

         //  printf(" arr[i] = %d ",arr[i]);
            ii=1;
           dd=arr[i]/b1;
          // printf("dd= %d ",dd);
           if(arr[i]%b1==0)
           {
               for(int j=0;;++j)
               {  // printf(" ii = %d  ",ii);
                    if(ii==dd)
                   { //printf(" bad word %d ",arr[i]);
                       bad++;break;
                   }
                   ii*=q;

                   if(abs(ii)>abs(dd))
                   {
                       break;
                   }
               }
           }
       }
       }

   while(1)
   { //printf("%d ",b1);

      if(abs(b1)<l)
      {
          counta++;//b1*=q;
      }
      else if(abs(b1)==l){counta++;break;}b1*=q;
      if(abs(b1)>l)break;

   }

   printf("%d\n",counta-bad);
   }

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pp pair<int,int>
#define mp make_pair
#define pb push_back
#define S second
#define F first

#define sf(x) scanf("%d",&x);
#define sff(x,y) scanf("%d%d",&x,&y);
 //string s1="sura";
const int MAX=2e5+100;
const int MOD=1e9+7;
//int freq[1000001];
map<int,int>m1;
int main()
{
   int b1,q,l,m,a,arr[100003];
   sff(b1,q);
   sff(l,m);
   for(int i=0;i<m;++i)
   {
       sf(arr[i]);
       m1[arr[i]]=1;

   }
   int counta=0;
   if(((q==1&&m1[b1]!=1&&(abs(b1)<=l))||(q==0&&m1[0]!=1)||(q==-1&&(m1[b1]!=1||m1[-b1]!=1)&&(abs(b1)<=l)))&&(abs(b1)<=l))
   {
       printf("inf\n");
   }
   else if(((q==1)&&(m1[b1]==1)||(q==0&&m1[b1]==1&&m1[0]==1)||(q==-1&&m1[b1]==1&&m1[-b1]==1))||abs(b1)>l)
   {
       printf("0\n");
   }
   else if(q==0&&m1[b1]!=1&&abs(b1)<=l&&m1[0]==1)
   {
       printf("1\n");

   }

   else if(abs(q)>1){
       // for(int i=0;i<)
       double dd;
       int ii=q,bad=0;
       for(int i=0;i<m;++i)
       { if(abs(arr[i])<=l)
       {

            ii=1;
           dd=(double)arr[i]/b1;
           if(floor(dd)==dd)
           {
               for(int j=0;;++j)
               {  if(ii==dd)
                   {// printf("%d ",arr[i]);
                       bad++;break;
                   }
                   ii*=q;

                   if(abs(ii)>arr[i])
                   {
                       break;
                   }
               }
           }
       }
       }

   while(1)
   { //printf("%d ",b1);

      if(abs(b1)<=l)
      {
          counta++;//b1*=q;
      }b1*=q;
      if(abs(b1)>l)break;
   }

   printf("%d\n",counta-bad);
   }

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pp pair<int,int>
#define mp make_pair
#define pb push_back
#define S second
#define F first

#define sf(x) scanf("%d",&x);
#define sff(x,y) scanf("%d%d",&x,&y);
 //string s1="sura";
const int MAX=2e5+100;
const int MOD=1e9+7;
//int freq[1000001];
map<int,int>m1;
int main()
{
   int b1,q,l,m,a,arr[100003];
   sff(b1,q);
   sff(l,m);
   for(int i=0;i<m;++i)
   {
       sf(arr[i]);
       m1[arr[i]]=1;

   }
   int counta=0;
   if((q==0||q==1)&&(m1[b1]!=1)&&(m1[0]!=1))
   {
       printf("inf\n");
   }
   else if((q==1)&&(m1[b1]==1))
   {
       printf("0\n");
   }
   else if(q==0&&m1[0]==1&&m1[b1]!=1)
   {
       printf("1\n");
   }
   else if(m1[b1]==1&&q==0)
   {
       printf("inf\n");
   }
   else{
       // for(int i=0;i<)
       double dd;
       int ii=q,bad=0;
       for(int i=0;i<m;++i)
       { ii=1;
           dd=(double)arr[i]/b1;
           if(floor(dd)==dd)
           {
               for(int j=0;j<=30;++j)
               {  if(ii==dd)
                   { //printf("%d ",arr[i]);
                       bad++;break;
                   }
                   ii*=q;

                   if(ii>arr[i])
                   {
                       break;
                   }
               }
           }
       }

   while(1)
   { //printf("%d ",b1);

      if(abs(b1)<=l)
      {
          counta++;//b1*=q;
      }b1*=q;
      if(abs(b1)>l)break;
   }

   printf("%d\n",counta-bad);
   }

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pp pair<int,int>
#define mp make_pair
#define pb push_back
#define S second
#define F first

#define sf(x) scanf("%d",&x);
#define sff(x,y) scanf("%d%d",&x,&y);
 //string s1="sura";
const int MAX=2e5+100;
const int MOD=1e9+7;
//int freq[1000001];
map<int,int>m1;
int main()
{
   int b1,q,l,m,a,arr[100003];
   sff(b1,q);
   sff(l,m);
   for(int i=0;i<m;++i)
   {
       sf(arr[i]);
       m1[arr[i]]=1;

   }
   int counta=0;
   if((q==1&&m1[b1]!=1&&(abs(b1)<=l))||(q==0&&m1[0]!=1)||(q==-1&&(m1[b1]!=1||m1[-b1]!=1)&&(abs(b1)<=l)))
   {
       printf("inf\n");
   }
   else if((q==1)&&(m1[b1]==1)||(q==0&&m1[b1]==1&&m1[0]==1)||(q==-1&&m1[b1]==1&&m1[-b1]==1))
   {
       printf("0\n");
   }
   else if(q==0&&m1[b1]!=1&&abs(b1)<=l&&m1[0]==1)
   {
       printf("1\n");

   }

   else if(abs(q)>1){
       // for(int i=0;i<)
       double dd;
       int ii=q,bad=0;
       for(int i=0;i<m;++i)
       { if(arr[i]<=l)
       {

            ii=1;
           dd=(double)arr[i]/b1;
           if(floor(dd)==dd)
           {
               for(int j=0;j<=30;++j)
               {  if(ii==dd)
                   {// printf("%d ",arr[i]);
                       bad++;break;
                   }
                   ii*=q;

                   if(ii>arr[i])
                   {
                       break;
                   }
               }
           }
       }
       }

   while(1)
   { //printf("%d ",b1);

      if(abs(b1)<=l)
      {
          counta++;//b1*=q;
      }b1*=q;
      if(abs(b1)>l)break;
   }

   printf("%d\n",counta-bad);
   }

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pp pair<int,int>
#define mp make_pair
#define pb push_back
#define S second
#define F first

#define sf(x) scanf("%d",&x);
#define sff(x,y) scanf("%d%d",&x,&y);
 //string s1="sura";
const int MAX=2e5+100;
const int MOD=1e9+7;
//int freq[1000001];
map<int,int>m1;
int main()
{
   int b1,q,l,m,a;
   int arr[100003];
   sff(b1,q);
   sff(l,m);
   for(int i=0;i<m;++i)
   {
       sf(arr[i]);
       m1[arr[i]]=1;

   }
   int counta=0;
   if(((q==1&&m1[b1]!=1&&(abs(b1)<=l))||(q==0&&m1[0]!=1)||(q==-1&&(m1[b1]!=1||m1[-b1]!=1)&&(abs(b1)<=l)))&&(abs(b1)<=l))
   {
       printf("inf\n");
   }
   else if(((q==1)&&(m1[b1]==1)||(q==0&&m1[b1]==1&&m1[0]==1)||(q==-1&&m1[b1]==1&&m1[-b1]==1))||abs(b1)>l)
   {
       printf("0\n");
   }
   else if(q==0&&m1[b1]!=1&&abs(b1)<=l&&m1[0]==1)
   {
       printf("1\n");

   }

   else if(abs(q)>1){
       // for(int i=0;i<)
       int dd;
       int ii,bad=0;
       for(int i=0;i<m;++i)
       {
          // printf("abs(arr[i]) %d ",abs(arr[i]));
          // long long a=arr[i];
           //printf("abs %d",abs(a));
          // if(arr[i]>0)

           if(abs(arr[i])<=l)
         {// printf("\n");

         //  printf(" arr[i] = %d ",arr[i]);
            ii=1;
           dd=arr[i]/b1;
          // printf("dd= %d ",dd);
           if(arr[i]%b1==0)
           {
               for(int j=0;;++j)
               {  // printf(" ii = %d  ",ii);
                    if(ii==dd)
                   { //printf(" bad word %d ",arr[i]);
                       bad++;break;
                   }
                   ii*=q;

                   if(abs(ii)>abs(dd))
                   {
                       break;
                   }
               }
           }
       }
       }

   while(1)
   { //printf("%d ",b1);

      if(abs(b1)<=l)
      {
          counta++;//b1*=q;
      }b1*=q;
      if(abs(b1)>l)break;
   }

   printf("%d\n",counta-bad);
   }

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pp pair<int,int>
#define mp make_pair
#define pb push_back
#define S second
#define F first

#define sf(x) scanf("%d",&x);
#define sff(x,y) scanf("%d%d",&x,&y);
 //string s1="sura";
const int MAX=2e5+100;
const int MOD=1e9+7;
//int freq[1000001];
map<int,int>m1;
int main()
{
   int b1,q,l,m,a;
   long long arr[100003];
   sff(b1,q);
   sff(l,m);
   for(int i=0;i<m;++i)
   {
       sf(arr[i]);
       m1[arr[i]]=1;

   }
   int counta=0;
   if(((q==1&&m1[b1]!=1&&(abs(b1)<=l))||(q==0&&m1[0]!=1)||(q==-1&&(m1[b1]!=1||m1[-b1]!=1)&&(abs(b1)<=l)))&&(abs(b1)<=l))
   {
       printf("inf\n");
   }
   else if(((q==1)&&(m1[b1]==1)||(q==0&&m1[b1]==1&&m1[0]==1)||(q==-1&&m1[b1]==1&&m1[-b1]==1))||abs(b1)>l)
   {
       printf("0\n");
   }
   else if(q==0&&m1[b1]!=1&&abs(b1)<=l&&m1[0]==1)
   {
       printf("1\n");

   }

   else if(abs(q)>1){
       // for(int i=0;i<)
       double dd;
       long long ii=q,bad=0;
       for(int i=0;i<m;++i)
       { if(abs(arr[i])<=l)
       {

            ii=1;
           dd=(double)arr[i]/b1;
           if(floor(dd)==dd)
           {
               for(int j=0;;++j)
               {  if(ii==dd)
                   {// printf("%d ",arr[i]);
                       bad++;break;
                   }
                   ii*=q;

                   if(abs(ii)>arr[i])
                   {
                       break;
                   }
               }
           }
       }
       }

   while(1)
   { //printf("%d ",b1);

      if(abs(b1)<=l)
      {
          counta++;//b1*=q;
      }b1*=q;
      if(abs(b1)>l)break;
   }

   printf("%d\n",counta-bad);
   }

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pp pair<int,int>
#define mp make_pair
#define pb push_back
#define S second
#define F first

#define sf(x) scanf("%d",&x);
#define sff(x,y) scanf("%d%d",&x,&y);
 //string s1="sura";
const int MAX=2e5+100;
const int MOD=1e9+7;
//int freq[1000001];
map<int,int>m1;
int main()
{
   int b1,q,l,m,a,arr[100003];
   sff(b1,q);
   sff(l,m);
   for(int i=0;i<m;++i)
   {
       sf(arr[i]);
       m1[arr[i]]=1;

   }
   int counta=0;
   if(((q==1&&m1[b1]!=1&&(abs(b1)<=l))||(q==0&&m1[0]!=1)||(q==-1&&(m1[b1]!=1||m1[-b1]!=1)&&(abs(b1)<=l)))&&(abs(b1)<=l))
   {
       printf("inf\n");
   }
   else if(((q==1)&&(m1[b1]==1)||(q==0&&m1[b1]==1&&m1[0]==1)||(q==-1&&m1[b1]==1&&m1[-b1]==1))||abs(b1)>l)
   {
       printf("0\n");
   }
   else if(q==0&&m1[b1]!=1&&abs(b1)<=l&&m1[0]==1)
   {
       printf("1\n");

   }

   else if(abs(q)>1){
       // for(int i=0;i<)
       double dd;
       int ii=q,bad=0;
       for(int i=0;i<m;++i)
       { if(arr[i]<=l)
       {

            ii=1;
           dd=(double)arr[i]/b1;
           if(floor(dd)==dd)
           {
               for(int j=0;;++j)
               {  if(ii==dd)
                   {// printf("%d ",arr[i]);
                       bad++;break;
                   }
                   ii*=q;

                   if(ii>arr[i])
                   {
                       break;
                   }
               }
           }
       }
       }

   while(1)
   { //printf("%d ",b1);

      if(abs(b1)<=l)
      {
          counta++;//b1*=q;
      }b1*=q;
      if(abs(b1)>l)break;
   }

   printf("%d\n",counta-bad);
   }

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pp pair<int,int>
#define mp make_pair
#define pb push_back
#define S second
#define F first

#define sf(x) scanf("%d",&x);
#define sff(x,y) scanf("%d%d",&x,&y);
 //string s1="sura";
const int MAX=2e5+100;
const int MOD=1e9+7;
//int freq[1000001];
map<int,int>m1;
int main()
{
   int b1,q,l,m,a,arr[100003];
   sff(b1,q);
   sff(l,m);
   for(int i=0;i<m;++i)
   {
       sf(arr[i]);
       m1[arr[i]]=1;

   }
   int counta=0;
   if(((q==1&&m1[b1]!=1&&(abs(b1)<=l))||(q==0&&m1[0]!=1)||(q==-1&&(m1[b1]!=1||m1[-b1]!=1)&&(abs(b1)<=l)))&&(abs(b1)<=l))
   {
       printf("inf\n");
   }
   else if(((q==1)&&(m1[b1]==1)||(q==0&&m1[b1]==1&&m1[0]==1)||(q==-1&&m1[b1]==1&&m1[-b1]==1))||abs(b1)>l)
   {
       printf("0\n");
   }
   else if(q==0&&m1[b1]!=1&&abs(b1)<=l&&m1[0]==1)
   {
       printf("1\n");

   }

   else if(abs(q)>1){
       // for(int i=0;i<)
       double dd;
       int ii=q,bad=0;
       for(int i=0;i<m;++i)
       { if(arr[i]<=l)
       {

            ii=1;
           dd=(double)arr[i]/b1;
           if(floor(dd)==dd)
           {
               for(int j=0;;++j)
               {  if(ii==dd)
                   {// printf("%d ",arr[i]);
                       bad++;break;
                   }
                   ii*=q;

                   if(abs(ii)>arr[i])
                   {
                       break;
                   }
               }
           }
       }
       }

   while(1)
   { //printf("%d ",b1);

      if(abs(b1)<=l)
      {
          counta++;//b1*=q;
      }b1*=q;
      if(abs(b1)>l)break;
   }

   printf("%d\n",counta-bad);
   }

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pp pair<int,int>
#define mp make_pair
#define pb push_back
#define S second
#define F first

#define sf(x) scanf("%d",&x);
#define sff(x,y) scanf("%d%d",&x,&y);
 //string s1="sura";
const int MAX=2e5+100;
const int MOD=1e9+7;
//int freq[1000001];
map<int,int>m1;
int main()
{
   int b1,q,l,m,a,k;
   int arr[100003];
   sff(b1,q);
   sff(l,m);
   for(int i=0;i<m;++i)
   {
       sf(arr[i]);
       m1[arr[i]]=1;

   }
   int counta=0;
   if(((q==1&&m1[b1]!=1&&(abs(b1)<=l))||(q==0&&m1[0]!=1)||(q==-1&&(m1[b1]!=1||m1[-b1]!=1)&&(abs(b1)<=l)))&&(abs(b1)<=l))
   {
       printf("inf\n");
   }
   else if(((q==1)&&(m1[b1]==1)||(q==0&&m1[b1]==1&&m1[0]==1)||(q==-1&&m1[b1]==1&&m1[-b1]==1))||abs(b1)>l)
   {
       printf("0\n");
   }
   else if(q==0&&m1[b1]!=1&&abs(b1)<=l&&m1[0]==1)
   {
       printf("1\n");

   }
   else if(b1==0&&m[b1]==1)
   {
       printf("0\n");
   }
   else if(b1==0&&m[b1]!=1)
   {
       printf("inf\n");
   }

   else if(abs(q)>1){
       // for(int i=0;i<)
       int dd;
       int ii,bad=0;
       for(int i=0;i<m;++i)
       {
          // printf("abs(arr[i]) %d ",abs(arr[i]));
          // long long a=arr[i];
           //printf("abs %d",abs(a));
          // if(arr[i]>0)

           if(abs(arr[i])<=l)
         {// printf("\n");

         //  printf(" arr[i] = %d ",arr[i]);
            ii=1;
           dd=arr[i]/b1;
          // printf("dd= %d ",dd);
           if(arr[i]%b1==0)
           {
               for(int j=0;;++j)
               {  // printf(" ii = %d  ",ii);
                    if(ii==dd)
                   { //printf(" bad word %d ",arr[i]);
                       bad++;break;
                   }
                   ii*=q;

                   if(abs(ii)>abs(dd))
                   {
                       break;
                   }
               }
           }
       }
       }

   while(1)
   { //printf("%d ",b1);

      if(abs(b1)<=l)
      {
          counta++;//b1*=q;
      }b1*=q;
      if(abs(b1)>l)break;
   }

   printf("%d\n",counta-bad);
   }

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pp pair<int,int>
#define mp make_pair
#define pb push_back
#define S second
#define F first

#define sf(x) scanf("%d",&x);
#define sff(x,y) scanf("%d%d",&x,&y);
 //string s1="sura";
const int MAX=2e5+100;
const int MOD=1e9+7;
//int freq[1000001];
map<int,int>m1;
int main()
{
   int b1,q,l,m,a;
   sff(b1,q);
   sff(l,m);
   for(int i=0;i<m;++i)
   {
       sf(a);
       m1[a]=1;

   }
   int counta=0;
   if((q==0||q==1)&&(m1[b1]!=1)&&(m1[0]!=1))
   {
       printf("inf\n");
   }
   else if((q==1)&&(m1[b1]==1))
   {
       printf("0\n");
   }
   else if(q==0&&m1[0]==1&&m1[b1]!=1)
   {
       printf("1\n");
   }
   else if(m1[b1]==1&&q==0)
   {
       printf("inf\n");
   }
   else{
   while(1)
   { //printf("%d ",b1);
      if(m1[b1]!=1&&b1<=l)
      {
          counta++;//b1*=q;
      }b1*=q;
      if(b1>l)break;
   }

   printf("%d\n",counta);
   }

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pp pair<int,int>
#define mp make_pair
#define pb push_back
#define S second
#define F first

#define sf(x) scanf("%d",&x);
#define sff(x,y) scanf("%d%d",&x,&y);
 //string s1="sura";
const int MAX=2e5+100;
const int MOD=1e9+7;
//int freq[1000001];
map<int,int>m1;
int main()
{
   int b1,q,l,m,a;
   int arr[100003];
   sff(b1,q);
   sff(l,m);
   for(int i=0;i<m;++i)
   {
       sf(arr[i]);
       m1[arr[i]]=1;

   }
   int counta=0;
   if(((q==1&&m1[b1]!=1&&(abs(b1)<=l))||(q==0&&m1[0]!=1)||(q==-1&&(m1[b1]!=1||m1[-b1]!=1)&&(abs(b1)<=l)))&&(abs(b1)<=l))
   {
       printf("inf\n");
   }
   else if(((q==1)&&(m1[b1]==1)||(q==0&&m1[b1]==1&&m1[0]==1)||(q==-1&&m1[b1]==1&&m1[-b1]==1))||abs(b1)>l)
   {
       printf("0\n");
   }
   else if(q==0&&m1[b1]!=1&&abs(b1)<=l&&m1[0]==1)
   {
       printf("1\n");

   }
   else if(b1==0&&m[b1]==1)
   {
       printf("0\n");
   }
   else if(b1==0&&m[b1]!=1)
   {
       printf("inf\n");
   }

   else if(abs(q)>1){
       // for(int i=0;i<)
       int dd;
       int ii,bad=0;
       for(int i=0;i<m;++i)
       {
          // printf("abs(arr[i]) %d ",abs(arr[i]));
          // long long a=arr[i];
           //printf("abs %d",abs(a));
          // if(arr[i]>0)

           if(abs(arr[i])<=l)
         {// printf("\n");

         //  printf(" arr[i] = %d ",arr[i]);
            ii=1;
           dd=arr[i]/b1;
          // printf("dd= %d ",dd);
           if(arr[i]%b1==0)
           {
               for(int j=0;;++j)
               {  // printf(" ii = %d  ",ii);
                    if(ii==dd)
                   { //printf(" bad word %d ",arr[i]);
                       bad++;break;
                   }
                   ii*=q;

                   if(abs(ii)>abs(dd))
                   {
                       break;
                   }
               }
           }
       }
       }

   while(1)
   { //printf("%d ",b1);

      if(abs(b1)<=l)
      {
          counta++;//b1*=q;
      }b1*=q;
      if(abs(b1)>l)break;
   }

   printf("%d\n",counta-bad);
   }

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pp pair<int,int>
#define mp make_pair
#define pb push_back
#define S second
#define F first

#define sf(x) scanf("%d",&x);
#define sff(x,y) scanf("%d%d",&x,&y);
 //string s1="sura";
const int MAX=2e5+100;
const int MOD=1e9+7;
//int freq[1000001];
map<int,int>m1;
int main()
{
   int b1,q,l,m,a,arr[100003];
   sff(b1,q);
   sff(l,m);
   for(int i=0;i<m;++i)
   {
       sf(arr[i]);
       m1[arr[i]]=1;

   }
   int counta=0;
   if(((q==1&&m1[b1]!=1&&(abs(b1)<=l))||(q==0&&m1[0]!=1)||(q==-1&&(m1[b1]!=1||m1[-b1]!=1)&&(abs(b1)<=l)))&&(abs(b1)<=l))
   {
       printf("inf\n");
   }
   else if(((q==1)&&(m1[b1]==1)||(q==0&&m1[b1]==1&&m1[0]==1)||(q==-1&&m1[b1]==1&&m1[-b1]==1))||abs(b1)>l)
   {
       printf("0\n");
   }
   else if(q==0&&m1[b1]!=1&&abs(b1)<=l&&m1[0]==1)
   {
       printf("1\n");

   }

   else if(abs(q)>1){
       // for(int i=0;i<)
       double dd;
       int ii=q,bad=0;
       for(int i=0;i<m;++i)
       { if(arr[i]<=l)
       {

            ii=1;
           dd=(double)arr[i]/b1;
           if(floor(dd)==dd)
           {
               for(int j=0;j<=30;++j)
               {  if(ii==dd)
                   {// printf("%d ",arr[i]);
                       bad++;break;
                   }
                   ii*=q;

                   if(ii>arr[i])
                   {
                       break;
                   }
               }
           }
       }
       }

   while(1)
   { //printf("%d ",b1);

      if(abs(b1)<=l)
      {
          counta++;//b1*=q;
      }b1*=q;
      if(abs(b1)>l)break;
   }

   printf("%d\n",counta-bad);
   }

}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int a[111111];
int main()
{
    int b,q,l,m;
    scanf("%d%d%d%d",&b,&q,&l,&m);
    for(int i=1;i<=m;i++)
        scanf("%d",a+i);
    if(abs(b)>l&&q>=1)
        printf("0\n");
    else if(!b)
    {
        int f=0;
        for(int i=1;i<=m;i++)
            if(!a[i])
        {
            f=1;
            break;
        }
        if(f)
           printf("0\n");
        else
           printf("inf\n");
    }
    else if(q==1)
    {
        int f=0;
        for(int i=1;i<=m;i++)
            if(a[i]==b)
        {
            f=1;
            break;
        }
        if(f)
           printf("0\n");
        else
           printf("inf\n");
    }
    else if(q==-1)
    {
        int f1=0,f2=0;
        for(int i=1;i<=m;i++)
        {
            if(a[i]==b&&!f1)
            {
                f1=1;
            }
            if(a[i]==-b&&!f2)
            {
                f2=1;
            }
            if(f1&&f2)
                break;
        }
        if(!f1||!f2)
            printf("inf\n");
        else
            printf("0\n");
    }
    else if(q==0)
    {
        int f=0;
        for(int i=1;i<=m;i++)
            if(!a[i])
        {
            f=1;
            break;
        }
        if(!f)
            printf("inf\n");
        else
        {
            f=0;
            for(int i=1;i<=m;i++)
            if(a[i]==b)
        {
            f=1;
            break;
        }
        if(f)
            printf("1\n");
        else
            printf("0\n");
        }
    }
    else
    {
        int ans=0;
        for(int i=b;abs(i)<=l;i*=q)
        {
        int f=0;
        for(int j=1;j<=m;j++)
            if(a[j]==i)
        {
            f=1;
            break;
        }
        if(!f)
           ans++;
        }
    printf("%d\n",ans);
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int a[11111];
int main()
{
    int b,q,l,m;
    scanf("%d%d%d%d",&b,&q,&l,&m);
    for(int i=1;i<=m;i++)
        scanf("%d",a+i);
    if(abs(b)>l&&q>=1)
        printf("0\n");
    else if(!b)
    {
        int f=0;
        for(int i=1;i<=m;i++)
            if(!a[i])
        {
            f=1;
            break;
        }
        if(f)
           printf("0\n");
        else
           printf("inf\n");
    }
    else if(q==1)
    {
        int f=0;
        for(int i=1;i<=m;i++)
            if(a[i]==b)
        {
            f=1;
            break;
        }
        if(f)
           printf("0\n");
        else
           printf("inf\n");
    }
    else if(q==-1)
    {
        int f=0;
        for(int i=1;i<=m;i++)
        {
            if(a[i]==b&&!f)
            {
                f++;
            }
            if(a[i]==-b&&f==1)
            {
                f++;
                break;
            }
        }
        if(f==2)
            printf("0\n");
        else
            printf("inf\n");
    }
    else if(q==0)
    {
        int f=0;
        for(int i=1;i<=m;i++)
            if(!a[i])
        {
            f=1;
            break;
        }
        if(!f)
            printf("inf\n");
        else
        {
            f=0;
            for(int i=1;i<=m;i++)
            if(a[i]==b)
        {
            f=1;
            break;
        }
        if(f)
            printf("1\n");
        else
            printf("0\n");
        }
    }
    else
    {
        int ans=0;
        for(int i=b;abs(i)<=l;i*=q)
        {
        int f=0;
        for(int j=1;j<=m;j++)
            if(a[j]==i)
        {
            f=1;
            break;
        }
        if(!f)
           ans++;
        }
    printf("%d\n",ans);
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int a[111111];
int main()
{
    int b,q,l,m;
    scanf("%d%d%d%d",&b,&q,&l,&m);
    for(int i=1;i<=m;i++)
        scanf("%d",a+i);
    if(abs(b)>l&&q>=1)
        printf("0\n");
    else if(!b)
    {
        int f=0;
        for(int i=1;i<=m;i++)
            if(!a[i])
        {
            f=1;
            break;
        }
        if(f)
           printf("0\n");
        else
           printf("inf\n");
    }
    else if(q==1)
    {
        int f=0;
        for(int i=1;i<=m;i++)
            if(a[i]==b)
        {
            f=1;
            break;
        }
        if(f)
           printf("0\n");
        else
           printf("inf\n");
    }
    else if(q==-1)
    {
        int f1=0,f2=0;
        for(int i=1;i<=m;i++)
        {
            if(a[i]==b&&!f1)
            {
                f1=1;
            }
            if(a[i]==-b&&!f2)
            {
                f2=1;
            }
            if(f1&&f2)
                break;
        }
        if(!f1&&!f2)
            printf("inf\n");
        else
            printf("0\n");
    }
    else if(q==0)
    {
        int f=0;
        for(int i=1;i<=m;i++)
            if(!a[i])
        {
            f=1;
            break;
        }
        if(!f)
            printf("inf\n");
        else
        {
            f=0;
            for(int i=1;i<=m;i++)
            if(a[i]==b)
        {
            f=1;
            break;
        }
        if(f)
            printf("1\n");
        else
            printf("0\n");
        }
    }
    else
    {
        int ans=0;
        for(int i=b;abs(i)<=l;i*=q)
        {
        int f=0;
        for(int j=1;j<=m;j++)
            if(a[j]==i)
        {
            f=1;
            break;
        }
        if(!f)
           ans++;
        }
    printf("%d\n",ans);
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int a[111111];
int main()
{
    int b,q,l,m;
    scanf("%d%d%d%d",&b,&q,&l,&m);
    for(int i=1;i<=m;i++)
        scanf("%d",a+i);
    if(abs(b)>l)
        printf("0\n");
    else if(!b)
    {
        int f=0;
        for(int i=1;i<=m;i++)
            if(!a[i])
        {
            f=1;
            break;
        }
        if(f)
           printf("0\n");
        else
           printf("inf\n");
    }
    else if(q==1)
    {
        int f=0;
        for(int i=1;i<=m;i++)
            if(a[i]==b)
        {
            f=1;
            break;
        }
        if(f)
           printf("0\n");
        else
           printf("inf\n");
    }
    else if(q==-1)
    {
        int f1=0,f2=0;
        for(int i=1;i<=m;i++)
        {
            if(a[i]==b&&!f1)
            {
                f1=1;
            }
            if(a[i]==-b&&!f2)
            {
                f2=1;
            }
            if(f1&&f2)
                break;
        }
        if(!f1||!f2)
            printf("inf\n");
        else
            printf("0\n");
    }
    else if(q==0)
    {
        int f=0;
        for(int i=1;i<=m;i++)
            if(!a[i])
        {
            f=1;
            break;
        }
        if(!f)
            printf("inf\n");
        else
        {
            f=0;
            for(int i=1;i<=m;i++)
            if(a[i]==b)
        {
            f=1;
            break;
        }
        if(f)
            printf("0\n");
        else
            printf("1\n");
        }
    }
    else if(abs(b)<=l&&q<1)
    {
        printf("inf\n");
    }
    else
    {
        int ans=0;
        for(int i=b;abs(i)<=l;i*=q)
        {
        int f=0;
        for(int j=1;j<=m;j++)
            if(a[j]==i)
        {
            f=1;
            break;
        }
        if(!f)
           ans++;
        }
    printf("%d\n",ans);
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int a[111111];
int main()
{
    int b,q,l,m;
    scanf("%d%d%d%d",&b,&q,&l,&m);
    for(int i=1;i<=m;i++)
        scanf("%d",a+i);
    if(abs(b)>l&&q>=1)
        printf("0\n");
    else if(!b)
    {
        int f=0;
        for(int i=1;i<=m;i++)
            if(!a[i])
        {
            f=1;
            break;
        }
        if(f)
           printf("0\n");
        else
           printf("inf\n");
    }
    else if(q==1)
    {
        int f=0;
        for(int i=1;i<=m;i++)
            if(a[i]==b)
        {
            f=1;
            break;
        }
        if(f)
           printf("0\n");
        else
           printf("inf\n");
    }
    else if(q==-1)
    {
        int f=0;
        for(int i=1;i<=m;i++)
        {
            if(a[i]==b&&!f)
            {
                f++;
            }
            if(a[i]==-b&&f==1)
            {
                f++;
                break;
            }
        }
        if(f==2)
            printf("0\n");
        else
            printf("inf\n");
    }
    else if(q==0)
    {
        int f=0;
        for(int i=1;i<=m;i++)
            if(!a[i])
        {
            f=1;
            break;
        }
        if(!f)
            printf("inf\n");
        else
        {
            f=0;
            for(int i=1;i<=m;i++)
            if(a[i]==b)
        {
            f=1;
            break;
        }
        if(f)
            printf("1\n");
        else
            printf("0\n");
        }
    }
    else
    {
        int ans=0;
        for(int i=b;abs(i)<=l;i*=q)
        {
        int f=0;
        for(int j=1;j<=m;j++)
            if(a[j]==i)
        {
            f=1;
            break;
        }
        if(!f)
           ans++;
        }
    printf("%d\n",ans);
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int a[111111];
int main()
{
    int b,q,l,m;
    scanf("%d%d%d%d",&b,&q,&l,&m);
    for(int i=1;i<=m;i++)
        scanf("%d",a+i);
    if(abs(b)>l)
        printf("0\n");
    else if(!b)
    {
        int f=0;
        for(int i=1;i<=m;i++)
            if(!a[i])
        {
            f=1;
            break;
        }
        if(f)
           printf("0\n");
        else
           printf("inf\n");
    }
    else if(q==1)
    {
        int f=0;
        for(int i=1;i<=m;i++)
            if(a[i]==b)
        {
            f=1;
            break;
        }
        if(f)
           printf("0\n");
        else
           printf("inf\n");
    }
    else if(q==-1)
    {
        int f1=0,f2=0;
        for(int i=1;i<=m;i++)
        {
            if(a[i]==b&&!f1)
            {
                f1=1;
            }
            if(a[i]==-b&&!f2)
            {
                f2=1;
            }
            if(f1&&f2)
                break;
        }
        if(!f1||!f2)
            printf("inf\n");
        else
            printf("0\n");
    }
    else if(q==0)
    {
        int f=0;
        for(int i=1;i<=m;i++)
            if(!a[i])
        {
            f=1;
            break;
        }
        if(!f)
            printf("inf\n");
        else
        {
            f=0;
            for(int i=1;i<=m;i++)
            if(a[i]==b)
        {
            f=1;
            break;
        }
        if(f)
            printf("0\n");
        else
            printf("1\n");
        }
    }
    else if(abs(b)<=l&&q<1&&q>0)
    {
        printf("inf\n");
    }
    else
    {
        int ans=0;
        for(long long i=b;(i<=l&&i>0)||(i>=-l&&i<0);i*=q)
        {
        int f=0;
        for(int j=1;j<=m;j++)
            if(a[j]==i)
        {
            f=1;
            break;
        }
        if(!f)
           ans++;
        }
    printf("%d\n",ans);
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int a[111111];
int main()
{
    int b,q,l,m;
    scanf("%d%d%d%d",&b,&q,&l,&m);
    for(int i=1;i<=m;i++)
        scanf("%d",a+i);
    if(abs(b)>l&&q>=1)
        printf("0\n");
    else if(!b)
    {
        int f=0;
        for(int i=1;i<=m;i++)
            if(!a[i])
        {
            f=1;
            break;
        }
        if(f)
           printf("0\n");
        else
           printf("inf\n");
    }
    else if(q==1)
    {
        int f=0;
        for(int i=1;i<=m;i++)
            if(a[i]==b)
        {
            f=1;
            break;
        }
        if(f)
           printf("0\n");
        else
           printf("inf\n");
    }
    else if(q==-1)
    {
        int f1=0,f2=0;
        for(int i=1;i<=m;i++)
        {
            if(a[i]==b&&!f1)
            {
                f1=1;
            }
            if(a[i]==-b&&!f2)
            {
                f2=1;
            }
            if(f1&&f2)
                break;
        }
        if(!f1||!f2)
            printf("inf\n");
        else
            printf("0\n");
    }
    else if(q==0)
    {
        int f=0;
        for(int i=1;i<=m;i++)
            if(!a[i])
        {
            f=1;
            break;
        }
        if(!f)
            printf("inf\n");
        else
        {
            f=0;
            for(int i=1;i<=m;i++)
            if(a[i]==b)
        {
            f=1;
            break;
        }
        if(f)
            printf("1\n");
        else
            printf("0\n");
        }
    }
    else if(abs(b)<=l&&q<1)
    {
        printf("inf\n");
    }
    else
    {
        int ans=0;
        for(int i=b;abs(i)<=l;i*=q)
        {
        int f=0;
        for(int j=1;j<=m;j++)
            if(a[j]==i)
        {
            f=1;
            break;
        }
        if(!f)
           ans++;
        }
    printf("%d\n",ans);
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int a[111111];
int main()
{
    int b,q,l,m;
    scanf("%d%d%d%d",&b,&q,&l,&m);
    for(int i=1;i<=m;i++)
        scanf("%d",a+i);
    if(abs(b)>l)
        printf("0\n");
    else if(!b)
    {
        int f=0;
        for(int i=1;i<=m;i++)
            if(!a[i])
        {
            f=1;
            break;
        }
        if(f)
           printf("0\n");
        else
           printf("inf\n");
    }
    else if(q==1)
    {
        int f=0;
        for(int i=1;i<=m;i++)
            if(a[i]==b)
        {
            f=1;
            break;
        }
        if(f)
           printf("0\n");
        else
           printf("inf\n");
    }
    else if(q==-1)
    {
        int f1=0,f2=0;
        for(int i=1;i<=m;i++)
        {
            if(a[i]==b&&!f1)
            {
                f1=1;
            }
            if(a[i]==-b&&!f2)
            {
                f2=1;
            }
            if(f1&&f2)
                break;
        }
        if(!f1||!f2)
            printf("inf\n");
        else
            printf("0\n");
    }
    else if(q==0)
    {
        int f=0;
        for(int i=1;i<=m;i++)
            if(!a[i])
        {
            f=1;
            break;
        }
        if(!f)
            printf("inf\n");
        else
        {
            f=0;
            for(int i=1;i<=m;i++)
            if(a[i]==b)
        {
            f=1;
            break;
        }
        if(f)
            printf("0\n");
        else
            printf("1\n");
        }
    }
    else if(abs(b)<=l&&q<1&&q>0)
    {
        printf("inf\n");
    }
    else
    {
        int ans=0;
        for(int i=b;abs(i)<=l;i*=q)
        {
        int f=0;
        for(int j=1;j<=m;j++)
            if(a[j]==i)
        {
            f=1;
            break;
        }
        if(!f)
           ans++;
        }
    printf("%d\n",ans);
    }
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int a[111111];
int main()
{
    int b,q,l,m;
    scanf("%d%d%d%d",&b,&q,&l,&m);
    for(int i=1;i<=m;i++)
        scanf("%d",a+i);
    if(abs(b)>l)
        printf("0\n");
    else if(!b)
    {
        int f=0;
        for(int i=1;i<=m;i++)
            if(!a[i])
        {
            f=1;
            break;
        }
        if(f)
           printf("0\n");
        else
           printf("inf\n");
    }
    else if(q==1)
    {
        int f=0;
        for(int i=1;i<=m;i++)
            if(a[i]==b)
        {
            f=1;
            break;
        }
        if(f)
           printf("0\n");
        else
           printf("inf\n");
    }
    else if(q==-1)
    {
        int f1=0,f2=0;
        for(int i=1;i<=m;i++)
        {
            if(a[i]==b&&!f1)
            {
                f1=1;
            }
            if(a[i]==-b&&!f2)
            {
                f2=1;
            }
            if(f1&&f2)
                break;
        }
        if(!f1||!f2)
            printf("inf\n");
        else
            printf("0\n");
    }
    else if(q==0)
    {
        int f=0;
        for(int i=1;i<=m;i++)
            if(!a[i])
        {
            f=1;
            break;
        }
        if(!f)
            printf("inf\n");
        else
        {
            f=0;
            for(int i=1;i<=m;i++)
            if(a[i]==b)
        {
            f=1;
            break;
        }
        if(f)
            printf("0\n");
        else
            printf("1\n");
        }
    }
    else if(abs(b)<=l&&q<1&&q>0)
    {
        printf("inf\n");
    }
    else
    {
        int ans=0;
        for(long long i=b;i<=l;i*=q)
        {
        int f=0;
        for(int j=1;j<=m;j++)
            if(a[j]==i)
        {
            f=1;
            break;
        }
        if(!f)
           ans++;
        }
    printf("%d\n",ans);
    }
    return 0;
}